gpt4 book ai didi

c++ - 在 char 数字中流式传输

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:18 25 4
gpt4 key购买 nike

有没有办法将数字流式传输到 unsigned char?

istringstream bytes( "13 14 15 16 17 18 19 20" );
unsigned char myChars[8];

for( int i = 0; i < 8 && !bytes.eof(); i++ )
{
bytes >> myChars[i];
cout << unsigned( myChars[i] ) << endl;
}

此代码当前输出前 8 个非空格字符的 ascii 值:

49 51 49 52 49 53 49 54

但是我想要的是每个token的数值:

13 14 15 16 17 18 19 20

最佳答案

你一次读取一个char,这意味着你得到'1''3',跳过空格,'1', '4', 跳过空格等

要将值读取为 NUMBERS,您需要使用整数类型作为临时值:

unsigned short s;
bytes >> s;
myChars[i] = s;

现在,流将读取一个整数值,例如13、14,存入s。然后使用 myChars[i] = s; 将其转换为 unsigned char

关于c++ - 在 char 数字中流式传输,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com