【发布时间】:2019-08-10 20:18:16
【问题描述】:
我有一个问题,我需要将字符串“\005”转换为无符号整数值 5。最好的方法是什么?
这是从 curl 响应中获取响应值。
int main()
{
char *ptr = "\005";
stringstream strValue;
strValue << *ptr;
unsigned int intValue;
strValue >> intValue;
cout << ptr << endl;
cout << intValue << endl;
return 0;
}
期望输出为 5 但得到
0
【问题讨论】:
-
这不是很明显吗?
unsigned int n=*ptr;? -
您是否有字符串
\005(即首先带有实际反斜杠)或[NUL]05(即第一个字符是ASCII0)字节? -
int intvalue = 5;会做到的。要获得更有用的答案,您需要解释该字符串中每个字符的含义,即值是如何编码的?值 6 将如何编码? 10? 33? -
"这是从 curl 响应中获取响应值。" - 我知道“\005”是来自http响应的字符串。所以需要解析才能得到 5
-
您正在尝试将编码为数字 5 的(不可打印的)字符解释为数字。 (行为就像你写了
char ptr[] = {5, 0};。)