od
doesn't show bytes by default, it shows words in octal. This may not quite be intuitive, but don't forget od
is a very old command :-) I'll use a somewhat simpler example than you did:
$ echo -en '\01\02' | od
0000000 001001
0000002
As Intel uses a little-endian architecture, the bytes \01\02
are interpreted as 00000010 00000001
in binary.
As octal digits each represent 3 bits, we can group that number like this:
(0)(000)(001)(000)(000)(001)
So the octal representation of those 2 bytes is:
001001
For day to day use this is pretty useless; perhaps back in the day it was handy for manually debugging memory dumps :-)
Your hello\n
example is:
h = 01101000
e = 01100101
l = 01101100
l = 01101100
o = 01101111
\n= 00001010
It's a bit more complicated now, because octal digits represent 3 bits, but bytes are 8 bits; so padding is added :-( The result symbollicaly is:
PehPllP\no
Remember, each set of 2 bytes is swapped due to the endianness. The P is a padding of 2 bits. The result in octal is (using a slash as separator):
00/01100101/01101000/00/01101100/01101100/00/00001010/01101111
Now in octal groups of 3 bits:
000 110 010 101 101 000 000 110 110 001 101 100 000 000 101 001 101 111
Translated into octal digits:
062550066154005157
This matches your result.
In conclusion you've probably learnt that od
without options is worse than useless :-)
\01\02
equal to two bytes only and0101
equal to four bytes? (the latter one makes sense to me) – user2820379 Nov 19 '14 at 16:45\01
is octal notation for one byte, it can range from\0
to\0377
. I don't know why you think0101
is equal to four bytes though. – wurtel Nov 24 '14 at 07:39