宽字符与单字符的互相转化
/*
*日期:2019.10.16
*功能:宽字符转多字节字符
*参数说明:
m_char[in]:多字符数组
m_wchar[out]:宽字符数组
*返回值:
*/
void WcharToChar( wchar_t * m_wchar, char *m_char)
{
int len = WideCharToMultiByte(CP_ACP, 0, m_wchar, -1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, m_wchar, -1, m_char, len, NULL, NULL);
}
/*
*日期:2019.10.16
*功能:多字节字符转宽字符
*参数说明:
m_char[in]:多字符数组
m_wchar[out]:宽字符数组
*返回值:
*/
void CharToWchar( wchar_t *m_wchar, char* m_char)
{
int len = MultiByteToWideChar(CP_ACP,0,m_char,-1,NULL,0);
MultiByteToWideChar(CP_ACP,0,m_char,-1,m_wchar,len);
}
asc字符数组与十六进制数组的转换
/*
*日期:2019.10.16
*功能:asc字符数组转化为十六进制数组
*参数说明:
cAsc[in]:asc字符数组
hexLen[in]:十六进制数组的长度
cHex[out]:十六进制数组
*返回值:
*/
void ascTohex( char *cAsc, int hexLen, char *cHex)
{
if(strlen((char*)cAsc) < hexLen*2)
{
char *asc_temp = new char[hexLen*2+1];
memset(asc_temp, 'F', hexLen*2+1);
memcpy(asc_temp, cAsc, strlen((char*)cAsc));
for(int i = 0; i < hexLen; i++)
{
char temp[3] = {asc_temp[i*2], asc_temp[i*2+1]};
cHex[i] = strtol(temp, 0, 16);
}
delete []asc_temp;
}
else
{
for(int i = 0; i < hexLen; i++)
{
char temp[3] = {cAsc[i*2], cAsc[i*2+1]};
cHex[i] = strtol(temp, 0, 16);
}
}
}
/*
*日期:2019.10.16
*功能:为十六进制数组向asc字符数组转化
*参数说明:
cAsc[in]:asc字符数组
hexLen[in]:十六进制数组的长度
cHex[out]:十六进制数组
*返回值:
*/
void hexToasc(unsigned char *cHex, int hexLen, unsigned char *cAsc)
{
for(int i = 0; i < hexLen; i++)
{
sprintf((char*)cAsc+2*i, "%02X", cHex[i]);
}
}
测试
char str[] = "31323334";
char hex[100] = {0};
ascTohex(str,4,hex);
//hex 1234,0x310x320x330x34
unsigned char str[100] = {0};
unsigned char hex[100] = {0x31,0x32,0x33,0x34};
hexToasc(hex, 4, str);
//str 31323334