实际工作中,常常会遇到需要把 0x12345678 转为 arrary[4] = {0x12,0x34,0x56,0x78} 或者反过来转换,下面造了几个轮子方便使用。
字节数组和整型互转
- 字节数组转为整型
-
- byte array to dword
- byte array to long
- byte array to word
- byte array to int
- 测试代码:
- 测试结果:
- 整型转为字节数组
-
- dword to byte array
- word to byte array
- 测试代码:
- 测试结果:
字节数组转为整型
byte array to dword
byte GBF_ConvertByteArrToDword(byte hexRawData[], byte length, dword& outDWord)
{
int i;byte retVal; if (length > 4){
retVal = gcNok;}else{
retVal = gcOk;}outDWord = 0;for(i=0;i<length;i++){
outDWord = outDWord << 8; outDWord = outDWord + hexRawData[i];}return retVal;
}
byte array to long
byte GBF_ConvertByteArrToLong(byte hexRawData[], byte length, long& outDWord)
{
int i;byte retVal; if (length > 4){
retVal = gcNok;}else{
retVal = gcOk;}//reset outputoutDWord = 0;for(i=0;i<length;i++){
outDWord = outDWord << 8; outDWord = outDWord + hexRawData[i];// outDWord= ~outDWord | 0x80; }return retVal;
}
byte array to word
byte GBF_ConvertByteArrToword(byte hexRawData[], byte length, word& outWord)
{
int i;byte retVal; if (length > 2){
retVal = gcNok;}else{
retVal = gcOk;}//reset outputoutWord = 0;for(i=0;i<length;i++){
outWord = outWord << 8; outWord = outWord + hexRawData[i]; }return retVal;
}
byte array to int
byte GBF_ConvertByteArrToword(byte hexRawData[], byte length, int& outWord)
{
int i;byte retVal; if (length > 2){
retVal = gcNok;}else{
retVal = gcOk;}//reset outputoutWord = 0;for(i=0;i<length;i++){
outWord = outWord << 8; outWord = outWord + hexRawData[i]; }return retVal;
}
测试代码:
{
byte in_byte_array[4]={
0x10,0x20,0x30,0x40};long out_long;dword out_dword;word out_word;int out_int; GBF_ConvertByteArrToLong(in_byte_array,4,out_long); GBF_ConvertByteArrToDword(in_byte_array,4,out_dword); GBF_ConvertByteArrToword(in_byte_array,2,out_word); GBF_ConvertByteArrToInt(in_byte_array,2,out_int); write("out_long = 0x%x ",out_long);write("out_dword = 0x%x ",out_dword);write("out_word = 0x%x ",out_word);write("out_int = 0x%x ",out_int);}
测试结果:
out_long = 0x10203040
out_dword = 0x10203040
out_word = 0x1020
out_int = 0x1020
整型转为字节数组
dword to byte array
byte GBF_ConvertDWordToByteArr(dword rawData, byte outByteArr[])
{
char tmpErrStr[255];byte retOkNok;dword mask = 0x000000FF; //Mask for masking out one byteint i;、const byte noOfBytes = 4;if (elcount(outByteArr)<noOfBytes ) //check that arraylength is at least 4{
snprintf(tmpErrStr, elcount (tmpErrStr), "GBF_ConvertDWordToByteArr: ERROR: Length of byte array is to short: %d. Length must be at least %d", elcount(tmpErrStr), 4);write (tmpErrStr);write(tmpErrStr);retOkNok = gcNok;}else{
retOkNok = gcOk;for(i=noOfBytes -1;i>=0;i--){
outByteArr[i] = rawData & mask; //Mask one byte and copy it to out arrayrawData = rawData >> 8; //Shift one byte}}return retOkNok;
}
word to byte array
byte GBF_ConvertWordToByteArr(word rawData, byte outByteArr[])
{
char tmpErrStr[255];byte retOkNok;word mask = 0x00FF; //Mask for masking out one byteint i;const byte noOfBytes = 2;if (elcount(outByteArr) < noOfBytes) //check that arraylength is at least 2{
snprintf(tmpErrStr, elcount (tmpErrStr), "GBF_ConvertWordToByteArr: ERROR: Length of byte array is to short: %d. Length must be at least %d", elcount(tmpErrStr), noOfBytes);write (tmpErrStr);write(tmpErrStr);retOkNok = gcNok;}else{
retOkNok = gcOk;for(i= noOfBytes-1;i>=0;i--){
outByteArr[i] = rawData & mask; //Mask one byte and copy it to out arrayrawData = rawData >> 8; //Shift one byte}}return retOkNok;
}
测试代码:
{
dword in_dword = 0x12345678;word in_word = 0x1234;byte out_byte_array_dword[4]; byte out_byte_array_word[2]; GBF_ConvertDWordToByteArr(in_dword,out_byte_array_dword); write("out_byte_array_dword ={0x%x,0x%x,0x%x,0x%x} ",out_byte_array_dword[0],out_byte_array_dword[1],out_byte_array_dword[2],out_byte_array_dword[3]); GBF_ConvertWordToByteArr(in_word,out_byte_array_word); write("out_byte_array_word ={0x%x,0x%x} ",out_byte_array_word[0],out_byte_array_word[1]); }
测试结果:
out_byte_array_dword ={
0x12,0x34,0x56,0x78}
out_byte_array_word ={
0x12,0x34}