欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

十六进制数组和二进制字符串的相互转换

程序员文章站 2024-03-16 22:56:58
...
例如 { 0x23, 0x4A, 0x5E } <=> “001000110100101001011110”
/***十六进制数转换成二进制字符串***/
void HexToBinStr(unsigned char* hexStr, unsigned char* binStr, int lenHex)
{    
    memset(binStr, '0', lenHex * 8);    
    unsigned char hexChar[2];    
    for (int i = 0; i < lenHex; i++)    
{        
hexChar[0] = (hexStr[i] & 0xF0) >> 4;        
hexChar[1] = hexStr[i] & 0x0F;        
for (int j = 0; j < 2; j++)        
{           
 for (int k = 0; k < 4; k++)            
{               
 if (hexChar[j] & (0x08 >> k))                
{                    
binStr[8 * i + 4 * j + k] = '1';                
}            
}        
}    
}
}

相关标签: MFC Windows Qt