转自大佬:https://blog.csdn.net/u010761559/article/details/83508834
1 int HexToAscii(unsigned char *pHexStr,unsigned char *pAscStr,int Len) 2 { 3 char Nibble[2]; 4 unsigned char Buffer[2048]; 5 int i = 0; 6 int j = 0; 7 8 for(i=0;i<Len;i++) 9 { 10 Nibble[0]=pHexStr[i] >> 4 & 0X0F; 11 Nibble[1]=pHexStr[i] & 0x0F; 12 for(j=0;j<2;j++) 13 { 14 if(Nibble[j]<10) 15 { 16 Nibble[j]=Nibble[j]+0x30; 17 } 18 else if(Nibble[j]<16) 19 { 20 Nibble[j]=Nibble[j]-10+'A'; 21 } 22 else 23 { 24 return 0; 25 } 26 } 27 memcpy(Buffer+i*2,Nibble,2); 28 } 29 Buffer[2*Len]=0x00; 30 memcpy(pAscStr,Buffer,2*Len); 31 pAscStr[2*Len]=0x00; 32 return 1; 33 } 34 }
1 char AsciiToHex(unsigned char * pAscii, unsigned char * pHex, int nLen) 2 { 3 int nHexLen = nLen / 2; 4 unsigned char Nibble[2] = {0}; 5 int i = 0; 6 int j = 0; 7 8 if (nLen%2) 9 { 10 return 1; 11 } 12 13 for (i = 0; i < nHexLen; i ++) 14 { 15 Nibble[0] = *pAscii ++; 16 Nibble[1] = *pAscii ++; 17 for (j = 0; j < 2; j ++) 18 { 19 if (Nibble[j] <= 'F' && Nibble[j] >= 'A') 20 Nibble[j] = Nibble[j] - 'A' + 10; 21 else if (Nibble[j] <= 'f' && Nibble[j] >= 'a') 22 Nibble[j] = Nibble[j] - 'a' + 10; 23 else if (Nibble[j] >= '0' && Nibble[j] <= '9') 24 Nibble [j] = Nibble[j] - '0'; 25 else 26 return 1;//Nibble[j] = Nibble[j] - 'a' + 10; 27 28 } // for (int j = ...) 29 pHex[i] = Nibble[0] << 4; // Set the high nibble 30 pHex[i] |= Nibble[1]; //Set the low nibble 31 } // for (int i = ...) 32 return 0; 33 }
1 char AsciiToDec(unsigned char * pAscii, unsigned char * pHex, int nLen) 2 { 3 int nHexLen = nLen / 2; 4 unsigned char Nibble[2] = {0}; 5 int i = 0; 6 int j = 0; 7 8 if (nLen%2) 9 { 10 return 1; 11 } 12 13 for (i = 0; i < nHexLen; i ++) 14 { 15 Nibble[0] = *pAscii ++; 16 Nibble[1] = *pAscii ++; 17 for (j = 0; j < 2; j ++) 18 { 19 Nibble [j] = Nibble[j] - '0'; 20 } // for (int j = ...) 21 22 pHex[i] = Nibble[0] * 10; // Set the high nibble 23 pHex[i] += Nibble[1]; //Set the low nibble 24 } // for (int i = ...) 25 return 0; 26 }
1 #define REVERSE(a) (((a) & 0xff) << 24 | ((a) & 0xff00) << 8 | ((a) & 0xff0000) >> 8 | ((a) & 0xff000000) >> 24) 2 //32位数据四个字节大小端转换