CAPL脚本中常用到的数据类型转换—— 整型数组(byte,int,long,dword array...
目录
- 整型数组(byte,int,long,dword)转为 Hex字符串
- Byte Array To Hex String
- 源代码
- 测试代码:
- 输出结果:
- Int Array To Hex String
- 源代码
- 测试代码:
- 输出结果:
- 总结
- Hex字符串转为 整型数组(byte,int,long,dword)
- Hex String To Byte Array
- 源代码
- 测试代码:
- 输出结果:
- Hex String To Int array
- 源代码
- 测试代码:
- 输出结果:
- 总结
整型数组(byte,int,long,dword)转为 Hex字符串
Byte Array To Hex String
源代码
byte GBF_Convert_ByteArrToHexStr(byte rawData[], dword datalen, char outHexStr[]) { word i; word hexLength; word byteIndex; byte tmpVal; byte retVal; char tmpStr[gcText10]; char tmpErrStr[gcText200]; const byte dataType = 2; // Init to failed retVal = gcNok; // Reset output array for (i = 0; i < elcount(outHexStr); i++) { outHexStr[i] = 0; } // get the hex length hexLength = datalen * dataType; //check that the supplied output array can hold the hex string if ( elcount(outHexStr) < hexLength ) { snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertIntArrToHexStr: ERROR: char array to small, Hex Data is %d but outHexStr only contains %d elements!', hexLength, elcount(outHexStr)); GBF_AddErrorInfo(tmpErrStr); } else { //All checks went fine, convert data for (i = 0; i < hexLength; i++) { // The byte index to use for accessing rawData byteIndex = i / dataType; tmpVal = ((byte)(rawData[byteIndex] >> (4 * (dataType -1 - (i % dataType))))) & 0x0F; // Convert value to a temp string. snprintf( tmpStr, elcount(tmpStr), '%X', tmpVal ); strncat(outHexStr, tmpStr, elcount(outHexStr)); if(i % dataType == dataType-1) strncat(outHexStr, ' ', elcount(outHexStr)); } retVal = gcOk; } return retVal; }
- 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
- 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
测试代码:
{
byte in_int_array[4]={0x10,0x20,0x30,0x40};
char out_char_array[40];
GBF_Convert_ByteArrToHexStr(in_int_array,4,out_char_array);
write('out_char_array = %s ',out_char_array);
}
1
2
3
4
5
6
1
2
3
4
5
6
输出结果:
out_char_array = 10 20 30 40
- 1
- 1
Int Array To Hex String
源代码
byte GBF_Convert_IntArrToHexStr(int rawData[], dword datalen, char outHexStr[])
{
word i;
word hexLength;
word byteIndex;
byte tmpVal;
byte retVal;
char tmpStr[gcText10];
char tmpErrStr[gcText200];
const byte dataType = 4;
// Init to failed
retVal = gcNok;
// Reset output array
for (i = 0; i < elcount(outHexStr); i++)
{
outHexStr[i] = 0;
}
// get the hex length
hexLength = datalen * dataType;
//check that the supplied output array can hold the hex string
if ( elcount(outHexStr) < hexLength )
{
snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertIntArrToHexStr: ERROR: char array to small, Hex Data is %d but outHexStr only contains %d elements!', hexLength, elcount(outHexStr));
GBF_AddErrorInfo(tmpErrStr);
}
else
{
//All checks went fine, convert data
for (i = 0; i < hexLength; i++)
{
// The byte index to use for accessing rawData
byteIndex = i / dataType;
tmpVal = ((byte)(rawData[byteIndex] >> (4 * (dataType -1 - (i % dataType))))) & 0x0F;
// Convert value to a temp string.
snprintf( tmpStr, elcount(tmpStr), '%X', tmpVal );
strncat(outHexStr, tmpStr, elcount(outHexStr));
if(i % dataType == dataType-1)
strncat(outHexStr, ' ', elcount(outHexStr));
}
retVal = gcOk;
}
return retVal;
}
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
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
测试代码:
{ int in_int_array[4]={0x1011,0x2022,0x3033,0x4044}; char out_char_array[20]; GBF_ConvertIntArrToHexStr(in_int_array,4,out_char_array); write('out_char_array = %s ',out_char_array); }
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
输出结果:
CAPL / .NETout_char_array = 1011 2022 3033 4044
1
2
1
2
总结
根据上面两个例子
只要将byte GBF_Convert_ByteArrToHexStr(byte rawData[], dword datalen, char outHexStr[])
改为byte GBF_Convert_LongArrToHexStr(long rawData[], dword datalen, char outHexStr[])
然后再将const byte dataType = 4;
改为const byte dataType = 8;
即可实现long 整型数组转为hex字符串。
Hex字符串转为 整型数组(byte,int,long,dword)
Hex String To Byte Array
源代码
byte GBF_ConvertHexStrToByteArray(char hexRawData[], byte outByteArr[]) { word i; word offset; word hexLength; byte tmpVal; byte retVal; char tmpErrStr[gcText200]; byte outdword; const byte dataType = 2; // Init to failed retVal = gcNok; offset = 0; // Reset output outdword = 0; // get the hex length hexLength = strlen(hexRawData); //remove possible '0x' at the beginning if( hexRawData[0] == '0' && hexRawData[1] == 'x' ) offset = 2; //check that the a dword (4 bytes) can hold the hex string if ( dataType < (hexLength - offset)/2 ) { snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertHexStrToInt: ERROR: Hex Data is %d which is more than a dword can hold!', hexLength); write ('Error in GBF_ConvertHexStrToInt: string is: %s',hexRawData); write(tmpErrStr); } else { retVal = gcOk; //All checks went fine, convert data for (i = offset; i < hexLength; i++) { outdword = outdword << 4; //shift the result // convert the Hex data and validity check it tmpVal = (byte)hexRawData[i]; if (tmpVal >= 0x30 && tmpVal <= 0x39) //0-9 tmpVal = tmpVal - 0x30; else if(tmpVal >= 'A' && tmpVal <= 'F') //A-F tmpVal = tmpVal - 0x37; else if (tmpVal >= 'a' && tmpVal <= 'f') //a-f tmpVal = tmpVal - 0x57; else { snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertHexStrToInt: ERROR: Invalid Hex data found in Hex string at position %d', i); write(tmpErrStr); retVal = gcNok; break; } outdword = outdword | tmpVal; //one nibble at a time.... if(i%dataType == dataType-1) outByteArr[i/dataType] = outdword; } return retVal; }
- 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
- 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
测试代码:
{
char in_char_array[5]='1234';
byte out_byte_array[20];
GBF_ConvertHexStrToByteArray(in_char_array,out_byte_array);
write('out_byte_array ={0x%x,0x%x} ',out_byte_array[0],out_byte_array[1]);
}
1
2
3
4
5
6
1
2
3
4
5
6
输出结果:
CAPL / .NETout_byte_array ={0x12,0x34}
- 1
- 1
Hex String To Int array
源代码
byte GBF_ConvertHexStrToIntArray(char hexRawData[], int outByteArr[])
{
word i;
word offset;
word hexLength;
byte tmpVal;
byte retVal;
char tmpErrStr[gcText200];
int outdword;
const byte dataType = 4;
// Init to failed
retVal = gcNok;
offset = 0;
// Reset output
outdword = 0;
// get the hex length
hexLength = strlen(hexRawData);
//remove possible '0x' at the beginning
if( hexRawData[0] == '0' && hexRawData[1] == 'x' )
offset = 2;
//check that the a dword (4 bytes) can hold the hex string
if ( dataType < (hexLength - offset)/2 )
{
snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertHexStrToInt: ERROR: Hex Data is %d which is more than a dword can hold!', hexLength);
write ('Error in GBF_ConvertHexStrToInt: string is: %s',hexRawData);
write(tmpErrStr);
}
else
{
retVal = gcOk;
//All checks went fine, convert data
for (i = offset; i < hexLength; i++)
{
outdword = outdword << 4; //shift the result
// convert the Hex data and validity check it
tmpVal = (byte)hexRawData[i];
if (tmpVal >= 0x30 && tmpVal <= 0x39) //0-9
tmpVal = tmpVal - 0x30;
else if(tmpVal >= 'A' && tmpVal <= 'F') //A-F
tmpVal = tmpVal - 0x37;
else if (tmpVal >= 'a' && tmpVal <= 'f') //a-f
tmpVal = tmpVal - 0x57;
else
{
snprintf(tmpErrStr, elcount (tmpErrStr), 'GBF_ConvertHexStrToInt: ERROR: Invalid Hex data found in Hex string at position %d', i);
write(tmpErrStr);
retVal = gcNok;
break;
}
outdword = outdword | tmpVal; //one nibble at a time....
if(i%dataType == dataType-1)
outByteArr[i/dataType] = outdword;
}
return retVal;
}
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
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
测试代码:
{ char in_char_array[9]='12345678'; int out_byte_array[20]; GBF_ConvertHexStrToIntArray(in_char_array,out_byte_array); write('out_byte_array ={0x%x,0x%x} ',out_byte_array[0],out_byte_array[1]); }
- 1
- 2
- 3
- 4
- 5
- 6
- 1
- 2
- 3
- 4
- 5
- 6
输出结果:
CAPL / .NETout_byte_array ={0x1234,0x5678}
1
1
总结
如果是想实现 Hex String To long array ,只需要将
byte GBF_ConvertHexStrToIntArray(char hexRawData[], int outByteArr[])
改为byte GBF_ConvertHexStrToLongArray(char hexRawData[], long outByteArr[])
int outdword; const byte dataType = 4;
改为long outdword; const byte dataType = 8;
赞 (0)