如何读取数字数据为uint8_t [英] How to read numeric data as uint8_t

查看:252
本文介绍了如何读取数字数据为uint8_t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 istream 中有一些人类可读的数值数据。值的范围是0-255,我想将它们存储在 uint8_t 中。不幸的是,如果我尝试像

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;

那么我最终以 a =='1' b =='2'。我知道这是在许多情况下所需的行为,但我想最终与 a == 124 b == 67 。我当前的解决方法是将数据流式传输到 int 中,然后将它们复制到 uint8_t

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_;

显然这会变得非常麻烦是否有更清洁的方法使用 s?

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?

推荐答案

你不能。 uint8_t int8_t unsigned char signed char 。这些类型被iostreams视为字符类型,并且没有办法改变这种行为。

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.

这篇关于如何读取数字数据为uint8_t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆