当前位置: 代码迷 >> 综合 >> CAPL脚本中常用到的数据类型转换—— 整型数组(byte,int,long,dword array)和 Hex字符串互转
  详细解决方案

CAPL脚本中常用到的数据类型转换—— 整型数组(byte,int,long,dword array)和 Hex字符串互转

热度:97   发布时间:2023-09-20 13:49:18.0

目录

  • 整型数组(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 failedretVal = gcNok;   // Reset output arrayfor (i = 0; i < elcount(outHexStr); i++){
    outHexStr[i] = 0;}// get the hex lengthhexLength = 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 datafor (i = 0; i < hexLength; i++){
    // The byte index to use for accessing rawDatabyteIndex = 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;
}

测试代码:

   {
    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);} 

输出结果:

out_char_array = 10 20 30 40  

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 failedretVal = gcNok;   // Reset output arrayfor (i = 0; i < elcount(outHexStr); i++){
    outHexStr[i] = 0;}// get the hex lengthhexLength = 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 datafor (i = 0; i < hexLength; i++){
    // The byte index to use for accessing rawDatabyteIndex = 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;
}

测试代码:

   {
    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);} 

输出结果:

CAPL / .NET	out_char_array = 1011 2022 3033 4044 

总结

根据上面两个例子
只要将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 failedretVal = gcNok;   offset = 0;// Reset output outdword = 0;   // get the hex lengthhexLength = strlen(hexRawData);//remove possible "0x" at the beginningif( 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 datafor (i = offset; i < hexLength; i++){
    outdword = outdword << 4;   //shift the result // convert the Hex data and validity check ittmpVal = (byte)hexRawData[i];if (tmpVal >= 0x30 && tmpVal <= 0x39)     //0-9tmpVal = tmpVal - 0x30;else if(tmpVal >= 'A' && tmpVal <= 'F')   //A-FtmpVal = tmpVal - 0x37;else if (tmpVal >= 'a' && tmpVal <= 'f')  //a-ftmpVal = 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;
}

测试代码:

   {
    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]);  } 

输出结果:

CAPL / .NET	out_byte_array ={
    0x12,0x34} 

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 failedretVal = gcNok;   offset = 0;// Reset output outdword = 0;   // get the hex lengthhexLength = strlen(hexRawData);//remove possible "0x" at the beginningif( 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 datafor (i = offset; i < hexLength; i++){
    outdword = outdword << 4;   //shift the result // convert the Hex data and validity check ittmpVal = (byte)hexRawData[i];if (tmpVal >= 0x30 && tmpVal <= 0x39)     //0-9tmpVal = tmpVal - 0x30;else if(tmpVal >= 'A' && tmpVal <= 'F')   //A-FtmpVal = tmpVal - 0x37;else if (tmpVal >= 'a' && tmpVal <= 'f')  //a-ftmpVal = 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;
}

测试代码:

   {
    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]); } 

输出结果:

CAPL / .NET	out_byte_array ={
    0x1234,0x5678} 

总结

如果是想实现 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;

  相关解决方案