1.16 binary char* turn binary char*
For example: "fedcba9876543210"
Converted to: Char bin[8] ={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10} form.
Code:
/* TwoCharacters into one character, the length of the original1/2*/static void Hex2char (char*szhex, unsigned char*rch){intI for(i=0; i<2; i++) {if(*(Szhex + i) >=' 0 '&&*(Szhex + i) <=' 9 ')*rch= (*rch<<4) + (*(Szhex + i)-' 0 ');Else if(*(Szhex + i) >=' A '&&*(Szhex + i) <=' F ')*rch= (*rch<<4) + (*(Szhex + i)-' A '+Ten);Else Break; }}
/*十六进制char* 转 Binary char*函数*/voidcharint iSize, char *pucCharStr){ int i; unsignedchar ch; if (iSize%20return; for(i=0; i<iSize/2; i++) { Hex2Char(pszHexStr+2*i, &ch); pucCharStr[i] = ch; }}
2. Binary char* to Hex char*
For example: Char bin[8] ={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
Convert to: "fedcba9876543210"
Code:
/ * Single character to hex string, increase length by 2 * /Static voidChar2hex (unsignedCharChChar*szhex) {intI UnsignedChar byte[2];byte[0] = ch/ -;byte[1] = ch% -; for(i=0; i<2; i++) {if(byte[I] >=0&&byte[I] <=9) Szhex[i] =' 0 '+byte[i];ElseSzhex[i] =' A '+byte[I]-Ten; } szhex[2] =0;}
/*字符串转换函数,中间调用上面的函数*/voidcharint iSize, char *pszHexStr){ int i; char szHex[3]; pszHexStr[00; for(i=0; i<iSize; i++) { Char2Hex(pucCharStr[i], szHex); strcat(pszHexStr, szHex); }}
Conversion of hexadecimal char* to binary char*