字符串与十六进制互相转换 vc
2009-06-15 11:07
190 查看
//字符串CString 转换成CString类型的十六进制串
**********************************************************************************
CString ConvertCStringoHex(CString Data)
{
//CString转换成char[]
wchar_t* a=Data.GetBuffer( Data.GetLength() );
int nLen = WideCharToMultiByte( CP_ACP, 0, a, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
char tagChar[2048];
WideCharToMultiByte( CP_ACP, 0, a, -1, pResult, nLen, NULL, NULL );
strncpy( tagChar,pResult , sizeof(tagChar));
//转换成hex
CString sResult=L"";
int nLoop=0;
while(tagChar[nLoop]!='/0')
{
static const char *hex="0123456789ABCDEF";
if(tagChar[nLoop]<127&&tagChar[nLoop]>0)
{
sResult += '0';
sResult += '0';
unsigned char chHexA = hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
unsigned char chHexB = hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
sResult += (char)chHexA;
sResult += (char)chHexB;
nLoop++;
}
else
{
unsigned char chHexA = hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
unsigned char chHexB = hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
sResult += (char)chHexA;
sResult += (char)chHexB;
if(tagChar[++nLoop]=='/0') break;
sResult+= hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
sResult+=hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
nLoop++;
}
}
return sResult;
}
*********************************************************************************
//十六进制转CString字符串(包括汉字)
CString ConvertHextoCString(CString hex)
{
CString result=_T("");
char temp[3];
int i=0;
if(hex.GetLength()%4!=0)
{
return _T("");
}
int lenhex=hex.GetLength();
while(i<lenhex)
{
long h4 = CharToHex((long)hex.GetAt(i));
long h3 = CharToHex((long)hex.GetAt(i+1));
long h2 = CharToHex((long)hex.GetAt(i+2));
long h1 = CharToHex((long)hex.GetAt(i+3));
long t1=h4*16+h3;
long t2=h2*16+h1;
if(t1==0)
{
temp[0]=t2;
temp[1]='/0';
}
else
{
temp[0]=t1;
temp[1]=t2;
temp[2]='/0';
}
i+=4;
result+=temp;
}
return result;
}
long CharToHex(long ch)
{
long la = (ch>=(long) 'A' ? (ch -(long) 'A' + 10) : (ch -(long) '0'));
return la;
}
**********************************************************************************
CString ConvertCStringoHex(CString Data)
{
//CString转换成char[]
wchar_t* a=Data.GetBuffer( Data.GetLength() );
int nLen = WideCharToMultiByte( CP_ACP, 0, a, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
char tagChar[2048];
WideCharToMultiByte( CP_ACP, 0, a, -1, pResult, nLen, NULL, NULL );
strncpy( tagChar,pResult , sizeof(tagChar));
//转换成hex
CString sResult=L"";
int nLoop=0;
while(tagChar[nLoop]!='/0')
{
static const char *hex="0123456789ABCDEF";
if(tagChar[nLoop]<127&&tagChar[nLoop]>0)
{
sResult += '0';
sResult += '0';
unsigned char chHexA = hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
unsigned char chHexB = hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
sResult += (char)chHexA;
sResult += (char)chHexB;
nLoop++;
}
else
{
unsigned char chHexA = hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
unsigned char chHexB = hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
sResult += (char)chHexA;
sResult += (char)chHexB;
if(tagChar[++nLoop]=='/0') break;
sResult+= hex[((unsigned char)(tagChar[nLoop]) >> 4) & 0x0f];
sResult+=hex[(unsigned char)(tagChar[nLoop]) & 0x0f];
nLoop++;
}
}
return sResult;
}
*********************************************************************************
//十六进制转CString字符串(包括汉字)
CString ConvertHextoCString(CString hex)
{
CString result=_T("");
char temp[3];
int i=0;
if(hex.GetLength()%4!=0)
{
return _T("");
}
int lenhex=hex.GetLength();
while(i<lenhex)
{
long h4 = CharToHex((long)hex.GetAt(i));
long h3 = CharToHex((long)hex.GetAt(i+1));
long h2 = CharToHex((long)hex.GetAt(i+2));
long h1 = CharToHex((long)hex.GetAt(i+3));
long t1=h4*16+h3;
long t2=h2*16+h1;
if(t1==0)
{
temp[0]=t2;
temp[1]='/0';
}
else
{
temp[0]=t1;
temp[1]=t2;
temp[2]='/0';
}
i+=4;
result+=temp;
}
return result;
}
long CharToHex(long ch)
{
long la = (ch>=(long) 'A' ? (ch -(long) 'A' + 10) : (ch -(long) '0'));
return la;
}
相关文章推荐
- VC字符串类型CString,int,char*,string,wchar_t互相转换代码实例(转自闪电博客)
- 字节数组和字符串按十六进制互相转换
- 十六进制和字符串互相转换 去掉字符串中的标点符号
- byte[]数组与十六进制字符串与字符串的互相转换
- VC中 十六进制字符串转换为十进制数字 转
- Java 字节数组与十六进制表示的字符串,互相转换
- 十六进制字符串与图片互相转换
- 字节数组和字符串按十六进制互相转换
- 字符串与十六进制互相转换 c#
- VC中 十六进制字符串转换为十进制数字
- vc 浮点数转换为内存十六进制字符串
- 字符串形式的十六进制与十进制互相转换
- [转贴]VC中 十六进制字符串转换为十进制数字
- MD5加密与byte[]数组与十六进制字符串与字符串的互相转换
- Java的十六进制与字符串之间互相转换函数(16进制与String转换)
- VC中字符串类型的互相转换
- VC字符串转换成十六进制函数StrToIntEx
- IOS中常用的字符串、十进制、二进制、十六进制之间互相转换及简单算术和
- VC 十六进制字符串转换为CString
- byte[]数组与十六进制字符串与字符串的互相转换(转载)