cstring 与unsigned char,请大家指教!

不易易 2018-07-05 10:15:54
我想用CString存储unsigned char,互相转换,如下:

CString str;
unsigned char uc[3]={0x99,0xff};
str.Format("%s",uc);

unsigned char ss[3]={0};
sscanf(str,"%s",uc);
我测试了一下,上面的互相转换没问题。
请大家看看,有没有潜在的危险?谢谢您!
...全文
180 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
不易易 2018-07-10
  • 打赏
  • 举报
回复
谢谢,各位,本人菜鸟,不该这么用
赵4老师 2018-07-06
  • 打赏
  • 举报
回复
#include <afxdisp.h>
#include <stdio.h>
int main() {
int i;
CString str;
unsigned char uc[6]={0x99,0xff,0x00,0xff,0x00,0xee};
str.Empty();
for (i=0;i<6;i++) {
str+=uc[i];
}

unsigned char ss[6]={0};
for (i=0;i<str.GetLength() && i<6;i++) {
ss[i]=str[i];
}
for (i=0;i<6;i++) {
printf("0x%02x,",ss[i]);
}
return 0;
}
//0x99,0xff,0x00,0xff,0x00,0xee,
不易易 2018-07-06
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
CString str;
unsigned char uc[3]={0x99,0xff,0};
str.Format("%s",uc);

unsigned char ss[3]={0};
sscanf(str,"%2s",ss);


老师的意思是,%s读到0就结束了?我试了下确实这样。

unsigned char uc[3]={0x99,0xff, 0x00, 0xff,0x00,0xee};
如果要传上面的字符,怎么写到cstring里呢?谢谢
str.Format("%s",uc);这样,只能取到前两个字节。遇到0就结束了。
不易易 2018-07-06
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
CString str;
unsigned char uc[3]={0x99,0xff,0};
str.Format("%s",uc);

unsigned char ss[3]={0};
sscanf(str,"%2s",ss);

老师的意思是,%s读到0就结束了?我试了下确实这样。

unsigned char uc[3]={0x99,0xff, 0x00, 0xff,0x00,0xee};
如果要传上面的字符,怎么写到cstring里呢?谢谢
赵4老师 2018-07-06
  • 打赏
  • 举报
回复
CString str;
unsigned char uc[3]={0x99,0xff,0};
str.Format("%s",uc);

unsigned char ss[3]={0};
sscanf(str,"%2s",ss);
smwhotjay 2018-07-06
  • 打赏
  • 举报
回复
CString unsigned char 如果保存的是可视化字符串,那么会没事,如果保存的unsigned char* 的二进制,那么CString 会遇到null截断,导致问题。
tiger波波 2018-07-06
  • 打赏
  • 举报
回复
为啥要用CString存储unsigned char ? CString通常用来显示,如果超出常见字符范围会显示乱码

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧