How to read numeric data as uint8_t [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-01 16:20:59

问题


I have some human-readable numeric data in an istream. The values range from 0-255, and I want to store them in uint8_t. Unfortunately, if I try something like

uint8_t a, b;
stringstream data("124 67");
data >> a >> b;

then I end up with a == '1' and b == '2'. I understand that this is the desired behavior in many situations, but I want to end up with a == 124 and b == 67. My current workaround is to stream the data into ints, then copy them to the uint8_ts.

uint8_t a, b;
int a_, b_;
stringstream data("124 67");
data >> a_ >> b_;
a = a_;
b = b_;

Clearly this gets very cumbersome (and slightly inefficient). Is there a cleaner way of reading numeric (as opposed to character) uint8_t data using streams?


回答1:


You can't. uint8_t and int8_t are typedefs for unsigned char and signed char respectively. These types are treated as character types by iostreams and there's no way to change that behaviour.

Your second example is really the only way you can do this.



来源:https://stackoverflow.com/questions/25277218/how-to-read-numeric-data-as-uint8-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!