CAPL脚本中常⽤到的数据类型转换——整型数组(byte,int,long,dwordar。。
。
⽬录
整型数组(byte,int,long,dword)转为 Hex字符串
Byte Array To Hex String
源代码
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 el ements!", 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));
}
idea配置tomcat不到serverretVal = gcOk;
}
苏州官方最新通告access常用函数用法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 =10203040
Int Array To Hex String
源代码
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 el ements!", 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;
}
测试代码:
{
int in_int_array[4]={0x1011,0x2022,0x3033,0x4044};
char out_char_array[20];
GBF_ConvertIntArrToHexStr(in_int_array,4,out_char_array);
文件格式转换appexcel中工龄计算最简单方法write("out_char_array = %s ",out_char_array);
}
输出结果:
CAPL /.NET out_char_array =1011202230334044
总结
根据上⾯两个例⼦
只要将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字符串。
excel中mod函数是什么意思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
if(i%dataType == dataType-1)
outByteArr[i/dataType]= outdword;
}
return retVal;
}
测试代码:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论