从流中读取十六进制文本格式 0x

发布于 2024-11-27 10:36:45 字数 1443 浏览 0 评论 0原文

我正在寻找一种简单的方法来使用流从文本文件中读取十六进制值。我使用“C++ 十六进制读取流 0x”搜索了 Stack Overflow,大多数响应都是关于将十六进制写入文本或读取不带“0x”前缀的十六进制值。本题是关于读取十六进制数字,在一次操作中以“0x”前缀作为数字。

我的方法:

unsigned char byte;
std::istringstream sample("0xce");
sample >> std::hex >> byte;

以第一个字符开始包含“0”(0x30) 的 byte 结束。

“strtol”函数处理转换,但需要读取数据,转换为 C 样式字符串。

我正在类中重载 operator>> 来读取逗号分隔值 (CSV) 文本文件。以下是数据文件的示例:

1,-4.93994892,0xa5,8,115.313e+3,
2,-4.93986238,0xc0,8,114.711e+3,
3,-4.93977554,0xc2,8,114.677e+3,

我的提取方法:

class Csv_Entry
{
public:
    friend std::istream& operator >>(std::istream& inp, Csv_Entry& ce);
    unsigned int    m_index;
    double      m_time;
    unsigned char   m_byte;
    unsigned int    m_data_length;
    double      m_bit_rate;
};

std::istream&
operator >> (std::istream& inp, Csv_Entry& ce)
{
    char    separator;
    inp >> ce.m_index;
    inp >> separator;
    inp >> ce.m_time;
    inp >> separator;
    inp >> std::hex >> ce.m_byte;
    inp >> separator;
    inp >> ce.m_data_length;
    inp >> separator;
    inp >> ce.m_bit_rate;
    inp.ignore(10000, '\n');
    return inp;
}

我需要使用 std::setw 吗?

编辑1:
我在 Windows 7 64 位平台上使用 Visual Studio 2010 Premium。

I'm looking for a simple method to read a hex value from a text file using streams. I searched Stack Overflow using "C++ hex read stream 0x" and most of responses were about writing hex as text or reading in hex values without the "0x" prefix. This question is about reading the hex number, with "0x" prefix as a number in one operation.

My method:

unsigned char byte;
std::istringstream sample("0xce");
sample >> std::hex >> byte;

Ends up with byte containing '0' (0x30) from the first character.

The 'strtol` function handles the conversion, but requires reading the data, converting to C-style string.

I'm overloading the operator>> in a class to read a comma separated value (CSV) text file. Here is a sample of the data file:

1,-4.93994892,0xa5,8,115.313e+3,
2,-4.93986238,0xc0,8,114.711e+3,
3,-4.93977554,0xc2,8,114.677e+3,

My extraction method:

class Csv_Entry
{
public:
    friend std::istream& operator >>(std::istream& inp, Csv_Entry& ce);
    unsigned int    m_index;
    double      m_time;
    unsigned char   m_byte;
    unsigned int    m_data_length;
    double      m_bit_rate;
};

std::istream&
operator >> (std::istream& inp, Csv_Entry& ce)
{
    char    separator;
    inp >> ce.m_index;
    inp >> separator;
    inp >> ce.m_time;
    inp >> separator;
    inp >> std::hex >> ce.m_byte;
    inp >> separator;
    inp >> ce.m_data_length;
    inp >> separator;
    inp >> ce.m_bit_rate;
    inp.ignore(10000, '\n');
    return inp;
}

Do I need to use std::setw?

Edit 1:
I'm using Visual Studio 2010 Premium on Windows 7, 64-bit platform.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-12-04 10:36:45

解决方案是使用 unsigned int 读取值,然后转换为 unsigned char

unsigned int value;
inp >> hex >> value;
unsigned char byte;
byte = value & 0xFF;

我猜有一些关于 unsigned char 类型的信息,即导致这个问题。

任何 C++ 语言律师都可以引用描述这种行为的部分吗?

A solution is to read the value using an unsigned int then convert to unsigned char:

unsigned int value;
inp >> hex >> value;
unsigned char byte;
byte = value & 0xFF;

I guess there is something about the type unsigned char that is causing the issue.

Any C++ language lawyers can quote a section that describes this behavior?

阳光的暖冬 2024-12-04 10:36:45

问题在于 Csv_Entrym_byte 成员的数据类型。当对输入数据进行输入流提取时,它将 0 解释为有效值,然后将 x 解释为分隔符,从而丢弃其余的流提取中的值。如果将 Csv_Entry::m_byte 成员更改为 unsigned int,问题就会消失,并且它会使用 std::hex< 正确解释十六进制值/代码>。

顺便说一句,由于您的所有成员都是公开的,您不妨将 Csv_Entry 设为一个结构,但这里是一些使用您的条目数据的示例工作代码:http://ideone.com/H7NG1

您会注意到在输出端,我只需要包含 std::hexstd::showbase 获取正确打印的十六进制值。

The problem is the data-type for your m_byte member of Csv_Entry. When going through input-stream extraction for your input data, it's interpreting the 0 as a valid value, then interpreting the x as a separator, and therefore throwing off the rest of the values in the stream-extraction. If you change your Csv_Entry::m_byte member to an unsigned int, the problem goes away, and it interprets the hex-value properly using std::hex.

BTW, since all your members are public, you might as well make Csv_Entry a structure, but here is some sample working code using your entry data: http://ideone.com/H7NG1

You'll notice on the output side, I only need to include std::hex and std::showbase to get the hex values to print correctly.

九命猫 2024-12-04 10:36:45

托马斯·马修斯对。您必须从 unsigned int 转换为 unsigned char

如果您熟悉 C 函数 scanf/printf,您会发现它们的行为相似。但我认为它们更能描述这种情况。

//%X specifies that we trying read integer in format 0x123FFF
//%c specifies that we trying read character
//0xABC is input string

unsigned char hex;
sscanf("0xABC", "%X", &hex); // error because not enough memory allocated
                             // by address &hex to store integer

unsigned char hex;
sscanf("0xABC", "%c", &hex); // reads only one character '0'

所以我的观点是,您可以读取十六进制整数或读取字符,但您试图“读取字符中的十六进制整数”。这是 stdlib 开发人员的具体情况)

Thomas Matthews right. You have to convert from unsigned int to unsigned char.

If you are convenient with C-functions scanf/printf you'll notice that they behave similar. But they are more descriptive for this situations I think.

//%X specifies that we trying read integer in format 0x123FFF
//%c specifies that we trying read character
//0xABC is input string

unsigned char hex;
sscanf("0xABC", "%X", &hex); // error because not enough memory allocated
                             // by address &hex to store integer

unsigned char hex;
sscanf("0xABC", "%c", &hex); // reads only one character '0'

So my point is that you can either read a hexadecimal integer or read character, but you trying to "read hexadecimal integer in character". So specific case for stdlib developers )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文