unionBase.cpp
3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//base.h 实现功能基本的函数
/*
* Copyright (c) 2011, 广州江南科友union
* All rights reserved.
* 文件名称: unionBase.cpp
* 摘 要: 实现功能基本的函数
* 当前版本: 1.0, 编写者: 杨武,修改时间: 2011-11-22 修改内容: 创建
*
*/
#include "stdafx.h"
#include "unionBase.h"
CString GetRandStr(int count)
{
int num = 0 ;
char temp[2] = {0};
CString str=_T("");
for(int i =0 ;i<count;i++)
{
num = rand()%9 + 1;
memset(temp,0,1);
sprintf_s(temp, "%d", num);
str += CString(temp);
}
return str;
}
char * my_strcat(char * _Dest,char * _Source,int &_dLen,int _sLen)
{
char *pD,*pS;
pD = _Dest;
pS = _Source;
pD = pD + _dLen;
for(int i=0;i<_sLen;i++)
{
*pD = *pS;
pD++;
pS++;
}
*pD = 0;
_dLen = _dLen+_sLen;
return _Dest;
}
int my_strncut(char * str_dest,char * str_source,int start_position, int len)
{
int i=start_position;
int j=0;
for(j=0;j<len;j++)
{
str_dest[j]=str_source[i];
i++;
}
return len;
}
void IntToChar(int iValue,int number,char *buf)
{
CString str;
str.Format(_T("%d"),iValue);
int j = str.GetLength();
for(int i=0;i<(number - j );i++)
{
str = _T("0") + str;
}
memcpy(buf,(char*)(LPCTSTR)str,number);
return;
}
void IntToHexChar(int iValue,int number,char *buf)
{
CString str;
str.Format(_T("%0X"),iValue);
int j = str.GetLength();
for(int i=0;i<(number - j );i++)
{
str = _T("0") + str;
}
memcpy(buf,(char*)(LPCTSTR)str,number);
return;
}
void lenToChar(const int len,char * keyLenType)
{
char keyLen[3] = {0};
switch(len)
{
case 16:
keyLen[0] = '0';
keyLen[1] = '1';
break;
case 32:
keyLen[0] = '0';
keyLen[1] = '2';
break;
case 48:
keyLen[0] = '0';
keyLen[1] = '3';
break;
default:
keyLen[0] = '0';
keyLen[1] = '2';
}
memcpy(keyLenType,keyLen,2);
}
int revValue(char * value)
{
CString strRev ;
int rev = 0;
strRev = CString(value);
strRev.TrimLeft(_T("0"));
if(!strRev.IsEmpty())
{
rev = 0 - _ttoi(strRev);
}
return rev;
}
int HexToDec(char* s)
{
char*p = s;
if(*p=='\0')
return 0;
while(*p=='0')
p++;
int dec = 0;
char c;
while(c=*p++)
{
dec<<=4;
if(c>='0'&&c<='9')
{
dec+=c-'0';
continue;
}
if(c>='a'&&c<='f')
{
dec+=c-'a'+10;
continue;
}
if(c>='A'&&c<='F')
{
dec+=c-'A'+10;
continue;
}
return -1;
}
return dec;
}
long HexToLDec(char* s)
{
char*p = s;
if(*p=='\0')
return 0;
while(*p=='0')
p++;
long dec = 0;
char c;
while(c=*p++)
{
dec<<=4;
if(c>='0'&&c<='9')
{
dec+=c-'0';
continue;
}
if(c>='a'&&c<='f')
{
dec+=c-'a'+10;
continue;
}
if(c>='A'&&c<='F')
{
dec+=c-'A'+10;
continue;
}
return -1;
}
return dec;
}
int getConfig(struct CONFIG *cfg)
{
cfg->hsmLenOfMsgHeader = GetPrivateProfileInt(_T("hsminfo"),_T("hsmmsgheadlen"),0,_T(".\\config.ini"));
cfg->port = GetPrivateProfileInt(_T("hsminfo"),_T("port"),8,_T(".\\config.ini"));
cfg->isLenOfHsmMsg = GetPrivateProfileInt(_T("hsminfo"),_T("isLenOfHsmMsg"),0,_T(".\\config.ini"));
cfg->timeout = GetPrivateProfileInt(_T("hsminfo"),_T("timeout"),3000,_T(".\\config.ini"));
GetPrivateProfileString(_T("hsminfo"),_T("ip"),_T("127.0.0.1"),cfg->ip,25,_T(".\\config.ini"));
return 0;
}