Read hexadecimal text format 0x from stream

I am 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 the answers concerned writing hexadecimal text or reading in hexadecimal values ​​without the "0x" prefix. This question is to read the hexadecimal number with the prefix "0x" as a number in one operation.

My method:

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

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

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

I overload operator>> in a class to read a delimited text file (CSV). Here is an example 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 ?

Change 1:
I am using Visual Studio 2010 Premium on a Windows 7 platform, 64-bit version.

+6
source share
3 answers

The solution is to read the value using unsigned int , then convert to unsigned char :

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

I think there is something like unsigned char causing the problem.

Can any C ++ lawyer cite a section describing this behavior?

+7
source

The problem is the data type for your m_byte member of Csv_Entry . After going through the extraction of the input stream for your input, it interprets 0 as a valid value, then interprets x as a delimiter, and therefore discards the rest of the values ​​in the extraction stream. If you change your Csv_Entry::m_byte to unsigned int , the problem Csv_Entry::m_byte away and it correctly interprets the hex value with std::hex .

BTW, since all your members are publicly available, you can also create the Csv_Entry structure, but here is an example of a working code using your input data: http://ideone.com/H7NG1

You will notice on the output side, I need to include std::hex and std::showbase in order to get the correct hex values.

0
source

Thomas Matthews . You must convert from unsigned int to unsigned char .

If you are comfortable with the scanf/printf C functions, you will notice that they behave the same. But they are more obvious for these 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, I want to say that you can either read the hexadecimal integer or read the character, but you are trying to "read the hexadecimal integer in the character". So a specific case for stdlib developers)

0
source

Source: https://habr.com/ru/post/894132/


All Articles