简体   繁体   English

如何将 ostringstream 十六进制字符串字符对转换为单个 unit8_t 等效二进制值

[英]how to convert an ostringstream hex string character pairs to a single unit8_t equivalent binary value

I am trying to do the following:我正在尝试执行以下操作:

I have an assembled ostringstream object that contains a hex payload to be transmitted.我有一个组装好的 ostringstream object,其中包含要传输的十六进制有效负载。 Say, it might be说,可能是

03125412349876543210af     (this is representing the data using hex convention in my string)

This string is representing 11 bytes, so for example the last byte to be transmitted is 0xaf (two characters giving me 8 bits of actual data).这个字符串代表 11 个字节,因此例如要传输的最后一个字节是 0xaf(两个字符给我 8 位实际数据)。

I wish to read each pair of characters, for example the '03' pair of characters in the string and convert that into a uint8_t element which I will push onto a vector of uint8_t elements.我希望读取每对字符,例如字符串中的“03”对字符并将其转换为 uint8_t 元素,我将其推送到 uint8_t 元素的向量上。 Essentially I will create a new vector of uint8_t elements based on the contents of the string, then transmit the vector.本质上,我将根据字符串的内容创建一个新的 uint8_t 元素向量,然后传输该向量。

My test program below works OK for 'int' but does not give me what I want for uint8_t.我下面的测试程序适用于“int”,但没有给我想要的 uint8_t。 Is there an elegant and/or straightforward way to do what I am trying to do that anyone can suggest?是否有一种优雅和/或直接的方式来做我想做的任何人都可以建议的事情?

(Note: example 3 was to see what happens if a non-hex-legal value was used. In example 1, a value like 34r0 would convert 34(hex) to an equivalent int and ignore the r and everything following it). (注意:示例 3 是要查看如果使用非十六进制合法值会发生什么。在示例 1 中,像 34r0 这样的值会将 34(hex) 转换为等效的 int 并忽略 r 及其后面的所有内容)。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    // example 1: making a hex string an int - works
    std::stringstream str1;
    std::string s1 = "5f";
    str1 << s1;
    int value1;
    str1 >> std::hex >> value1;
    cout << value1 << endl; // produces 95 - perfect

    cout << "~~~~~~~~~" << endl;

    // example 2: making a hex string a uint8 - not the result I want
    std::stringstream str2;
    std::string s2 = "5f";
    str2 << s2;
    uint8_t value2;
    str2 >> std::hex >> value2;
    cout << value2 << endl; // produces 5 - not what I want!

    cout << "~~~~~~~~~~~" << endl;

    // example 3: using non-hex values
    std::stringstream str3;
    std::string s3 = "wx";
    str3 << s3;
    uint8_t value3;
    str3 >> std::hex >> value3;
    cout << value3 << endl; // produces w - not what I want!

    // this does not work either
    uint8_t value4;
    cout << "~~~~~~~~~~~~~~" << endl;
    value4 = (uint8_t)value1;
    cout << value4 << endl; // produces - - not what I want!

    cout << "............." << endl;


    return 0;
}

The output from this test program looks like the below:此测试程序中的 output 如下所示:

Hello world!
95
~~~~~~~~~
5
~~~~~~~~~~~
w
~~~~~~~~~~~~~~
_
.............

Process returned 0 (0x0)   execution time : 0.022 s
Press any key to continue.

Example 1 works OK, but using int - this is not what I need.示例 1 工作正常,但使用 int - 这不是我需要的。

In example 2 you are extracting a uint8_t , 8 bits, so one char from the string, the 5 .在示例 2 中,您将提取uint8_t 8 位,因此从字符串中提取一个char 5

In example 3 is the same, you extract the first char, so the w .在示例 3 中是相同的,您提取第一个字符,因此w

For the last example, it prints a char (8 bits), 95 is the ASCII - character.对于最后一个示例,它打印一个char (8 位), 95是 ASCII -字符。 If you want to show the number, cast the value to int .如果要显示数字,请将值castint

value4 = (uint8_t)value1;
cout << (int)value4 << endl;

Thanks to Manuel for his answer.感谢曼努埃尔的回答。 This is one way to solve the problem.这是解决问题的一种方法。 I'd still like to know if there is a more elegant way to do this.我仍然想知道是否有更优雅的方式来做到这一点。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    // example 1: making a hex string an int - works
    std::stringstream str1;
    std::string s1 = "5f";
    str1 << s1;
    int value1;
    str1 >> std::hex >> value1;
    cout << "value 1 as int: " << value1 << endl; // produces 95 int - OK but an int
    uint8_t value2;
    value2 = (uint8_t)value1;
    cout << "value 2 as uint8: " << (int)value2 << endl; // produces 01011111 = 95 or '-' - correct

    return 0;
}

Which produces:产生:

Hello world!
value 1 as int: 95
value 2 as uint8: 95

Process returned 0 (0x0)   execution time : 0.022 s
Press any key to continue.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM