Commandline hexdump with ASCII output?
hexdump -C
does what you want.
# hexdump -C /etc/passwd
00000000 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a |root:x:0:0:root:|
00000010 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a |/root:/bin/bash.|
00000020 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 |daemon:x:1:1:dae|
00000030 6d 6f 6e 3a 2f 75 73 72 2f 73 62 69 6e 3a 2f 62 |mon:/usr/sbin:/b|
00000040 69 6e 2f 73 68 0a 62 69 6e 3a 78 3a 32 3a 32 3a |in/sh.bin:x:2:2:|
00000050 62 69 6e 3a 2f 62 69 6e 3a 2f 62 69 6e 2f 73 68 |bin:/bin:/bin/sh|
...
Printing hexadecimal characters in C
You are seeing the ffffff
because char
is signed on your system. In C, vararg functions such as printf
will promote all integers smaller than int
to int
. Since char
is an integer (8-bit signed integer in your case), your chars are being promoted to int
via sign-extension.
Since c0
and 80
have a leading 1-bit (and are negative as an 8-bit integer), they are being sign-extended while the others in your sample don't.
char int
c0 -> ffffffc0
80 -> ffffff80
61 -> 00000061
Here's a solution:
char ch = 0xC0;
printf("%x", ch & 0xff);
This will mask out the upper bits and keep only the lower 8 bits that you want.
Reading file in hex gives wrong output (skips some hex values)
Operator>> reads formatted data (char in your case) from the input stream, so a char with value 12 (0c in hex) is considered as ASCII form feed and ignored ( as a value of 32 or 0x20 would be considered a space ).
Char are also signed values and an hex value of a2 (which is decimal 162) would be stored as a negative value. Promoting that value to an int would keep the sign and due to complementation, when printed as unsigned you'll get the ffff's.
keep the
unsigned char input;
then to read the values use
inf.get(static_cast<char>(input))
and to print
cout << hex << setw(2) << setfill('0') << static_cast<unsigned int>(input) << " ";
Conversion hex string into ascii in bash command line
This worked for me.
$ echo 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$
-r
tells it to convert hex to ascii as opposed to its normal mode of doing the opposite
-p
tells it to use a plain format.
Ascii/Hex convert in bash
The reason is because hexdump
by default prints out 16-bit integers, not bytes. If your system has them, hd
(or hexdump -C
) or xxd
will provide less surprising outputs - if not, od -t x1
is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1c
to show both the byte hex values and the corresponding letters.
If you have xxd
(which ships with vim), you can use xxd -r
to convert back from hex (from the same format xxd
produces). If you just have plain hex (just the '4161', which is produced by xxd -p
) you can use xxd -r -p
to convert back.
Related Topics
Python Module Import Error for One User But Not the Other
How to Format My Grep Output to Show Line Numbers at the End of the Line, and Also the Hit Count
Bash Shell Script - Check for a Flag and Grab Its Value
How to Use Nohup to Run Process as a Background Process in Linux
What Are My Environment Variables
How to Set Up Autocompletion for Git Commands
Shell Script Error Expecting "Do"
How to Know the Interrupt/Gpio Number for a Specific Pin in Linux
How to Parse /Proc/Pid/Cmdline
Grabbing Specific Sections of Text from a String
How to Check If a Service That I Don't Know the Name of Is Running on Ubuntu
Symbolic Link to a Hook in Git
Internals of a Linux System Call
In Screen, How to Send a Command to All Virtual Terminal Windows Within a Single Screen Session
How to Tell Which Unix Shell I am Using