How does one write the hex values of a char in ASCII to a text file?
45,114
Solution 1
#include <iostream>
using namespace std;
int main() {
char c = 123;
cout << hex << int(c) << endl;
}
Edit: with zero padding:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
char c = 13;
cout << hex << setw(2) << setfill('0') << int(c) << endl;
}
Solution 2
char upperToHex(int byteVal)
{
int i = (byteVal & 0xF0) >> 4;
return nibbleToHex(i);
}
char lowerToHex(int byteVal)
{
int i = (byteVal & 0x0F);
return nibbleToHex(i);
}
char nibbleToHex(int nibble)
{
const int ascii_zero = 48;
const int ascii_a = 65;
if((nibble >= 0) && (nibble <= 9))
{
return (char) (nibble + ascii_zero);
}
if((nibble >= 10) && (nibble <= 15))
{
return (char) (nibble - 10 + ascii_a);
}
return '?';
}
More code here.
Solution 3
Try:
#include <iomanip>
....
stream << std::hex << static_cast<int>(buf[i]);
Solution 4
You can also do it using something a bit more old-fashioned:
char buffer[4];//room for 2 hex digits, one extra ' ' and \0
sprintf(buffer,"%02X ",onebyte);
Author by
xian
Updated on March 26, 2020Comments
-
xian about 4 years
Here is what I currently have so far:
void WriteHexToFile( std::ofstream &stream, void *ptr, int buflen, char *prefix ) { unsigned char *buf = (unsigned char*)ptr; for( int i = 0; i < buflen; ++i ) { if( i % 16 == 0 ) { stream << prefix; } stream << buf[i] << ' '; } }
I've tried doing stream.hex, stream.setf( std::ios::hex ), as well as searching Google for a bit. I've also tried:
stream << stream.hex << (int)buf[i] << ' ';
But that doesn't seem to work either.
Here is an example of some output that it currently produces:
Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í Í
I would like the output to look like the following:
FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00 FF EE DD CC BB AA 99 88 77 66 55 44 33 22 11 00
-
mmmmmmmm about 15 yearsBTW: You should use const void *ptr and const char *prefix to make clear that you won't modify these buffers.
-
John Doe about 15 yearsthis is why I like stack overflow so much. these fun little problems come up from time to time and someone drops in a snippet of code and its solved...
-
-
xian about 15 yearsWon't that just output it to the console?
-
Rob Kennedy about 15 yearsCould you explain why that should work any differently from using stream.hex, like the asker already tried?
-
Arnold Spence about 15 yearsI find the 'hex' stream modifier a bit of a pain. If you are outputting other types you have to keep switching the stream back (using 'dec' for example) and as far as I know, it doesn't do leading zeros.
-
Admin about 15 years@kitchen - replace cout with your own stream.
-
Rob Kennedy about 15 yearsOK, I'll explain it myself. Using stream.hex prints the value of the member-function pointer ostream::hex. (For me the value is 8.) Then it prints the integer value of the character in decimal.
-
Admin about 15 years@rob the hex manipul;ator is not a member of the stream class. The hex you are printing is acually an enumeration value
-
Mr.Ree about 15 yearsYou might want to use unsigned(c) instead of int(c) if c<0.
-
RBerteig about 15 yearsAnd this is why I love C++ streams: cout << hex << setw(2) << setfill('0') << int(c) << endl is just so much clearer than printf("%02x", c) ;-) I know its typesafe(er) but couldn't they have dreamed up something less grotesque?
-
Edouard A. about 15 yearsClarity is a question of point of view... I don't think printf("%02x", c) can be considered as "clear"
-
Lightness Races in Orbit almost 13 years-1 This is wrong. How has it avoided downvotes for over two years?!
-
Mooing Duck over 12 years@RBerteig: nitpick: those aren't quite equivalent, the C++ shouldn't have the
endl
there. -
Neo over 6 yearsWith c value being 197, it prints ffffffc5. Can anybody explain why it's not printing C5 and how to avoid extra 'f' padding?