首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

unsigned char*转CStrng解决思路

2013-04-02 
unsigned char*转CStrng有一个unsigned char* ptemp指针所指的数据为0x00, 0x01, 0x2, 0xff...我想用CStri

unsigned char*转CStrng
有一个unsigned char* ptemp指针所指的数据为0x00, 0x01, 0x2, 0xff...
我想用CString显示出来"0x00 0x01 0x02 0x0ff"
自动在每个字节前增加0x及显示出来. 我现在用
stemp.Format(_T("BLE sent: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X"), ptemp[0], ptemp[1], ....
可以显示, 但一定要知道字节长度. 有没有其他简单方法
[解决办法]
知道指针指向的内存块大小吧?那就用循环啊!
[解决办法]
或者,有结尾标记,也可以在循环中判断结尾标记以结束循环。
[解决办法]
一个字节一个字节的Format就行了,至少代码中不用定死长度了

CString strResult, strTemp;
strResult = _T("BLE sent: ");
for (int i = 0; i < nLen; i++)
{
    strTemp.Format(_T("0x%02X"), ptemp[i]);
    strResult += strTemp;
}

热点排行