-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToHexString.cc
More file actions
80 lines (65 loc) · 1.61 KB
/
StringToHexString.cc
File metadata and controls
80 lines (65 loc) · 1.61 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_DEST_STRING_LENGTH 100
#ifdef ENABLE_DEBUG_PRINT
#define PRINT printf
#else
#define PRINT
#endif
unsigned char charToHexChar(unsigned char ch)
{
if (ch <= 'F' && ch >= 'A') {
return (ch - 'A') + 10;
} else if (ch <= 'f' && ch >= 'a') {
return (ch - 'a') + 10;
} else if (ch <= '9' && ch >= '0') {
return (ch - '0');
} else {
PRINT("unknow character 0x%x", ch);
return 0xFF;
}
}
char stringToHexString(unsigned char *pSrcStr, unsigned int len, unsigned char *pDestStr, unsigned int *pOutLen)
{
if ((len <= 0) && (len % 2 != 0)) {
return -1;
}
// check string to sure it is hex string
for(int i = 0; i < len; i++) {
if (0xFF == charToHexChar(pSrcStr[i])) {
PRINT("input string not hex string, please check it!");
return -1;
}
}
//cover to hex
for(int i = 0; i < len; i+=2) {
pDestStr[i/2] = charToHexChar(pSrcStr[i]) * 16 + charToHexChar(pSrcStr[i+1]);
}
// output hex string
printf("Dest Hex String : 0x");
*pOutLen = len/2;
for(int i = 0; i < *pOutLen; i++) {
printf("%02X", pDestStr[i]);
}
return 0;
}
int main(int argc, char* argv[])
{
unsigned char destStr[MAX_DEST_STRING_LENGTH] = {0xFF};
unsigned int destStrLen = 0;
if(argc < 1) {
PRINT("Using as: 'C00100080000010000FF02'\n");
return -1;
} else {
PRINT("argc %d, len %d: ", argc, strlen(argv[1]));
for(int i = 0; i < strlen(argv[1]); i++) {
PRINT("%C", argv[1][i]);
}
PRINT("\r\n");
}
if(-1 == stringToHexString((unsigned char *)argv[1], strlen(argv[1]), destStr, &destStrLen)) {
PRINT("conveter failed!");
return -1;
}
}