0

I couldn't find anyone with a similar problem so I decided to create a new question.

I am currently trying to extract hexadecimal 1 byte sized values from a string stream, I plan on storing the value extracted inside an unsigned char, the problem is, the string stream will extract a single character from the stream and store it in my variable. That's understandable, I thought, as I successfully used a short integer to extract my value and it worked flawlessly. Now that I knew my problem, I thought I could simply cast my variable to a numerical value, while keeping it as an unsigned char, but apparently it wasn't possible for me to cast it as the right operand of the extraction operator:

Note: byte is a typedef for unsigned char, not std::byte.

Here are some attempts:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

I'm fine using a short instead of a char, but I wanted to know if there's a way around this, using member operator>>() didn't work for me either!

Edit: This is how my stringstream string looks like: "11 A7 D9 13 6D 9D ED DA FD F4 F1". I want to extract the hexadecimal 11 (17 decimal) into my byteBuffer variable.

Luiz
  • 148
  • 1
  • 9
  • The overload isn't for `short`, it is for `short &`, since you when you read from input you want to write into the variable. – 1201ProgramAlarm Nov 20 '20 at 22:00
  • Sorry that I wasn't clear, I meant my value ranges from 0x00 up to 0xFF, values that can be stored inside a single 8 bit byte. – Luiz Nov 20 '20 at 22:02
  • For the "odd error by the way", see [No operator “>>” matches these operands operand types are: std::istream >> double](https://stackoverflow.com/questions/64298188/) – JaMiT Nov 20 '20 at 22:02
  • Using a `short` to extract `A7` worked flawlessly? (Even extracting the `11` and getting `17` stored in your `short` seems suspicious. Are you using a stream setting that you have not shown us?) – JaMiT Nov 20 '20 at 22:04
  • @john I have edited the question and added what kind of input I'm dealing with at the bottom, I have a string with hexadecimal values < 0xFF separated by space characters. – Luiz Nov 20 '20 at 22:05
  • 2
    I don't think there is any way to do this using a byte variable directly. Just read into a short and then assign the short to the byte. – john Nov 20 '20 at 22:05
  • @Luiz yes, got it now. – john Nov 20 '20 at 22:05
  • 1
    Don't put pictures of code/text. Include it as formatted text in the question instead. – super Nov 20 '20 at 22:11
  • 1
    Just read into any non-character integral type, and then after extraction is complete, convert the value and put it where you want it. – Ben Voigt Nov 20 '20 at 22:31
  • @BenVoigt I just ended up using a short integer altogether, I never had a problem doing it this way as I said, I just wanted to know if there was something I was missing here. – Luiz Nov 20 '20 at 22:36
  • In my opinion, using `short` is suitable and there is nothing missing. – Barrnet Chou Nov 24 '20 at 09:41

1 Answers1

1

You have to read both characters and do the conversion. You might create a class to handle it, something like:

struct Byte
{
    std::uint8_t c;  
};

std::istream& operator >> (std::istream& is, Byte& b)
{
    unsigned int n;
    is >> std::hex >> n;
    b.c = n;
    return is;
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Or just use `std::hex` and read into an `unsigned` local variable, then after reading, store it to the `byte` variable. – Ben Voigt Nov 20 '20 at 22:29
  • @BenVoigt: Indeed, better. changed. – Jarod42 Nov 20 '20 at 22:35
  • I thought of using overloading, but decided against it since it adds too much unnecessary complexity for what I need. Would work well nonetheless. – Luiz Nov 20 '20 at 22:38