可以将有符号字符数组转换为无符号整数吗?
我有一个像这样转换为有符号字符的无符号整数,
unsigned int b = 128;
char a[4];
a[0] = b >> 24;
a[1] = b >> 16;
a[2] = b >> 8;
a[3] = b >> 0;
在不知道 b
的值是什么的情况下,我可以取回该数字吗?对于大于 128 的数字,下面的方法会失败。从数组中获取数字似乎存在一些歧义。
unsigned int c = 0;
c += a[0] << 24;
c += a[1] << 16;
c += a[2] << 8;
c += a[3];
cout<<c<<endl;
I have a unsigned int that was converted to a signed char like this
unsigned int b = 128;
char a[4];
a[0] = b >> 24;
a[1] = b >> 16;
a[2] = b >> 8;
a[3] = b >> 0;
Without knowing what value of b
is, can I get back the number? The method below fails for numbers greater than 128. It seems like there is some ambiguity to getting the number back from the array.
unsigned int c = 0;
c += a[0] << 24;
c += a[1] << 16;
c += a[2] << 8;
c += a[3];
cout<<c<<endl;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
or
不建议将
signed
转换为unsigned
。如果您确实想要执行此操作,则必须手动执行。不容易,据我所知。看看这个问题。
Converting
signed
tounsigned
is not advised. If you do want to do it, you have to do it manually. Not easy, AFAIK.Have a look at this question.