字符串字符显示与16进制显示之间的相互转换
2012-10-08 19:54
405 查看
字符串字符显示与16进制显示之间的相互转换.
四个有用的函数:
//Function to convert unsigned char to string of length 2
void CXXX::Char2Hex(unsigned char ch, char* szHex)
{
unsigned char byte[2];
byte[0] = ch/16;
byte[1] = ch%16;
for (int i=0;i<2;i++)
{
if (byte[i]>=0 && byte[i]<=9)
{
szHex[i] ='0'+byte[i];
}
else if (byte[i]>='a' && byte[i]<='f')
{
szHex[i]= 'a'+byte[i]-10;
}
else
{
szHex[i]= 'A'+byte[i]-10;
}
}
szHex[2] = 0;
}
//Function to convert string of length 2 to unsigned char
void CXXX::Hex2Char(char * szHex, unsigned char& rch)
{
rch = 0;
for(int 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' + 10);
else
break;
}
}
//Function to convert string of unsigned chars to string of chars
void CXXX::CharStr2HexStr(unsigned char * pucCharStr, char* pszHexStr, int iSize)
{
int i;
char szHex[3];
pszHexStr[0] = 0;
for(i=0; i<iSize; i++)
{
Char2Hex(pucCharStr[i], szHex);
strcat(pszHexStr, szHex);
}
}
//Function to convert string of chars to string of unsigned chars
void CXXX::HexStr2CharStr(char * pszHexStr, unsigned char* pucCharStr, int iSize)
{
int i;
unsigned char ch;
for(i=0; i<iSize; i++)
{
Hex2Char(pszHexStr+2*i, ch);
pucCharStr[i] = ch;
}
}
四个有用的函数:
//Function to convert unsigned char to string of length 2
void CXXX::Char2Hex(unsigned char ch, char* szHex)
{
unsigned char byte[2];
byte[0] = ch/16;
byte[1] = ch%16;
for (int i=0;i<2;i++)
{
if (byte[i]>=0 && byte[i]<=9)
{
szHex[i] ='0'+byte[i];
}
else if (byte[i]>='a' && byte[i]<='f')
{
szHex[i]= 'a'+byte[i]-10;
}
else
{
szHex[i]= 'A'+byte[i]-10;
}
}
szHex[2] = 0;
}
//Function to convert string of length 2 to unsigned char
void CXXX::Hex2Char(char * szHex, unsigned char& rch)
{
rch = 0;
for(int 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' + 10);
else
break;
}
}
//Function to convert string of unsigned chars to string of chars
void CXXX::CharStr2HexStr(unsigned char * pucCharStr, char* pszHexStr, int iSize)
{
int i;
char szHex[3];
pszHexStr[0] = 0;
for(i=0; i<iSize; i++)
{
Char2Hex(pucCharStr[i], szHex);
strcat(pszHexStr, szHex);
}
}
//Function to convert string of chars to string of unsigned chars
void CXXX::HexStr2CharStr(char * pszHexStr, unsigned char* pucCharStr, int iSize)
{
int i;
unsigned char ch;
for(i=0; i<iSize; i++)
{
Hex2Char(pszHexStr+2*i, ch);
pucCharStr[i] = ch;
}
}
相关文章推荐
- 字符串字符显示与16进制显示之间的相互转换
- 字符串字符显示与16进制显示之间的相互转换
- 将char字符转换成int整型,字符串与整型之间的相互转换
- 字符串之间相互转换 字符与数值之间的转换
- 完成一程序演示字符数组、C风格字符串、std::string和MFC中的Cstring类型之间的相互转换
- 字符串,字符数组,字符串数组之间的相互转换
- 字符串转10进制,10进制转字符串,并移16进制数显示
- Java中字符和byte数组之间的相互转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换操作
- *Delphi字符串与字符数组之间的转换(初始化的重要性)
- python进阶---python字符串和整形的相互转换和多个字符分割(split)字符串
- java中进行2、8、16、10进制之间的转换
- C#数字进制间与字符串类型相互转换
- Delphi字符串、PChar与字符数组之间的转换
- javascript 中字符和ASCII之间的相互转换
- C#进制之间的相互转换详解
- OC (IOS) 字符串 字符 NSData相互转换(NSStrng char NSData)
- Delphi字符串、PChar与字符数组之间的转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- utf,unicode,ascii字符串之间的相互转换