关于强制数据类型转换的问题 例如CString Str="0xD1"; 怎么转换成unsigned char 类型的数据;在线等待,3Q3Q33Q 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 将CString型转换为LPBYTE(即unsigned char*)型的数据类型LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; for(int i=0;(i<str.GetLength())&&(*(lpb+i) = str.GetAt(i));i++)return lpb;} 我的意思是把十六进制数据0xD1转化成unsigned char 数据。 ??自己算最简单。或者把0x去掉,然后用sscanf("%X") unsigned char *p;CString str;int length=str.GetLength();for(int i=0;i<length;i++)p[i]=str.GetAt(i); 自己写个函数吧,还比较麻烦啦!!得到第三和第四个字符,判断第三和第四个是什么字符用switch case,然后再转换呗,我只会这种笨办法 请继续跟贴。我的问题还是没有解决,BCB中只要用函数Strtoint就可以了 int HexToInt(char* p){ int acc=0; if(*p++!='0') return -1; if(*p != 'x' && *p != 'X') return -1; p++; while(*p) { int bit4; if(*p >= '0' && *p <= '9') bit4 = *p - '0'; else if(*p >= 'a' && *p <= 'f') bit4 = *p - 'a' + 10; else if(*p >= 'A' && *p <= 'F') bit4 = *p - 'A' + 10; acc = (acc << 4) | bit4; p++; } return acc;} ////16进制字符串转换成数字UINT64 GetHexValue(CString str){ UINT64 m_value; char m_valueStr[255]; strcpy(m_valueStr,str); int m_currentLength = strlen(m_valueStr); char *p = m_valueStr; m_value = 0; for (int i = 0; i < m_currentLength; i++) { m_value *= 16; if (*p >= '0' && *p <= '9') m_value += (*p - '0'); else if (*p >= 'a' && *p <= 'f') m_value += (10 + *p - 'a'); else if (*p >= 'A' && *p <= 'F') m_value += (10 + *p - 'A'); p++; } return m_value ;} CString Str="0xD1";unsigned char uch;sscanf((LPCTSTR)Str,"0x%X",&uch); 做过TAPI的都进来看看!!!! VISTA 驱动开发相关 趴求:windows2000驱动程序开发指南电子书一本! 请教大家一个傻瓜 问题,但我是真的不懂。谢谢 请问用wise for windows installer怎么获得用户安装的目录 一个关于链表的问题? 这是不是CString的一个BUG?? 高手来看!!50分!! 我有个问题急待解决 串口通信编程问题 哪位朋友知道有这样的软件:我想装一软件,想监测此软件安装时到底装了什么文件,在什么位置 FormView问题??????????
LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE[str.GetLength()+1];
for(int i=0;(i<str.GetLength())&&(*(lpb+i) = str.GetAt(i));i++)
return lpb;
}
自己算最简单。
或者把0x去掉,然后用sscanf("%X")
CString str;
int length=str.GetLength();
for(int i=0;i<length;i++)
p[i]=str.GetAt(i);
用switch case,然后再转换呗,
我只会这种笨办法
{
int acc=0;
if(*p++!='0')
return -1;
if(*p != 'x' && *p != 'X')
return -1;
p++;
while(*p)
{
int bit4;
if(*p >= '0' && *p <= '9')
bit4 = *p - '0';
else if(*p >= 'a' && *p <= 'f')
bit4 = *p - 'a' + 10;
else if(*p >= 'A' && *p <= 'F')
bit4 = *p - 'A' + 10;
acc = (acc << 4) | bit4;
p++;
}
return acc;
}
UINT64 GetHexValue(CString str)
{
UINT64 m_value;
char m_valueStr[255];
strcpy(m_valueStr,str);
int m_currentLength = strlen(m_valueStr);
char *p = m_valueStr;
m_value = 0;
for (int i = 0; i < m_currentLength; i++)
{
m_value *= 16;
if (*p >= '0' && *p <= '9')
m_value += (*p - '0');
else if (*p >= 'a' && *p <= 'f')
m_value += (10 + *p - 'a');
else if (*p >= 'A' && *p <= 'F')
m_value += (10 + *p - 'A');
p++;
}
return m_value ;
}
unsigned char uch;sscanf((LPCTSTR)Str,"0x%X",&uch);