0

I am trying to use a stringstream to read through hex representation of serialized data which I can get from a DB or a file, eg "11007B000000..." so if I call a function called ReadShort the first 4 characters are interpreted as a short, then if I call a function called ReadInteger the remaining 8 characters as int, and so on. As of now I've tried istream, load a fstream from memory, opening stringstrem in binary mode, but I can't get the desired output. This is my test code:

#include <iostream>
#include <sstream>

int main()
{
    std::string baseString = "11007B0000003A0400000A";
    std::stringstream baseStream(baseString);
    std::stringstream binaryStream(baseString, std::stringstream::out | std::stringstream::in | std::stringstream::binary);

    signed short result;
    // First attempt
    baseStream >> std::hex >> result;
    std::cout << result << std::endl;

    result = 0;
    binaryStream >> std::hex >> result;
    std::cout << result << std::endl;

    // Second Attempt
    baseStream.seekg(0, baseStream.beg);
    binaryStream.seekg(0, binaryStream.beg);
    result = 0;
    baseStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    result = 0;
    binaryStream.read((char*)&result, sizeof(result));
    std::cout.write((const char*)&result, sizeof(result));

    // Third attempt
    result = 0;
    baseStream >> result;
    std::cout << result;

    result = 0;
    binaryStream >> result;
    std::cout << result;
}

Is there a better approach or is there something I'm missing?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    You say the first `4` chars are a `short` and the next `8` are an `int` but that's only 12 chars and your string has 22. What is your expected result here? Keep in mind that 2 chars of a hex string is one byte of a numeric value. – 001 Jun 21 '22 at 18:58
  • 1
    Do you have to use `stringstream`? Can you use other options like `string::substr()` and `strtol()`? – 001 Jun 21 '22 at 19:03
  • Be capable of cast the buffer with any data type including char array, not always the first characters are `short` and not the full buffer are used. In other example I have to read Integer, Double and Short the remaining characters are not used for casting. – orlando9427 Jun 21 '22 at 19:04
  • The problem with >> is that it takes as many characters/digits as it can as long as they match the type of variable. So `short x; stringstream s("1234506789"); s>>x;` will not just `x=12345` because that's all that fits into x - it will overfill x and give you the wrong answer. Putting a non numeric character (like space) inbetween the two numbers makes it work. But that doesn't help you. – Jerry Jeremiah Jun 21 '22 at 21:54
  • This answer has a good suggestion - use the read() function of the stringstream to get a certain number of characters: https://stackoverflow.com/questions/38226121/copy-specific-number-of-characters-from-stdbasic-istream-to-stdstring – Jerry Jeremiah Jun 21 '22 at 21:56
  • If you absolutely must use only stringstream instead of strtol and others: https://onlinegdb.com/P1cqBHQE1 – Jerry Jeremiah Jun 21 '22 at 22:18
  • Thanks both of you, I ended using a custom class containing a stringsteam where on constructor the source buffer is converted to bytes based on this topic https://stackoverflow.com/questions/17261798/converting-a-hex-string-to-a-byte-array Then just implemented operator>> for each specific data type – orlando9427 Jul 01 '22 at 00:51

0 Answers0