I’m wondering how to convert a hex string to a human readable string (if that makes any sense) this would be my first real encounter with hex values so I’m still learning about them and how to manage them.
I have a program which is reading in data from a file which contains raw packet data (hex) and I need to parse this information so it’s human readable.
An example of what I need to do is something like this site does http://home2.paulschou.net/tools/xlate/ where you can put in hex and have it converted to text.
The C++-ish way to get a string containing the hexadecimal representation of a given number is to use the
hexmodifier for streams, as in this example:You can use the same modifier on string streams in case you need to have the hexadecimal representation in a string variable:
UPDATE:
If your input data is given as a string containing the hexadecimal representation of the characters of a string, you will need to know the encoding of the input string in order to display it correctly. In the simplest case, the string is something like ASCII which maps one byte to one character. So in a given input “414243”, every two characters (“41”, “42”, “43) map to an ASCII value (65, 66, 67), which map to a character (“A”, “B”, “C”).
Here’s how to that in C++:
Using
std::strtolfrom<cstdlib>makes this easy; if you insist on using a template class for this, use std::stringstream to perform the conversion of the single sub strings (like “41”) to decimal values (65).