od and byte order
Objectives
We would like to :
- Understand and use the program
od
on *nix systems.
- Understand that the byte order in memory might not match the byte order in standard two's complement.
Notes
- References
- It is reasonable to look at files on the system in something other than "ascii" mode.
- Mostly files you work with contain ascii data.
- But not all.
- Create a file, letters.dat, with the letters in it.
-
cat letters.dat
, what does this show?
- Is there a way I could look at ascii values, the values in the file, not the character interpretation?
- Introduction od
- This is an old unix utility that allows us the examine the contents of a file in different formats.
-
od -c letters.dat
- The -c option displays in "ascii", note the \n
-
od -x letters.dat
- the -x says to print in hex format.
- But why are the letters partially reversed?
- Intel processors are little-endian
- Memory is just an array of bytes.
- M[0] stores a byte, M[1] stores a byte
- A character, is 8 bits or two hex digits.
- So this is stored in a single byte.
- in the previous od, we saw
6261 6463
- And this represents
ba dc
- So the individual bytes are fine,
- But the byte order is wrong (strange, challenging, ...).
- As we discussed earlier, in Intel we often think of a word as 16 bits or 4 bytes. (this is not necessarily true, but ...)
- Little endian means:
- Intel represents the "first" byte or MSB at position n and the LSB or least byete as byte 0
- Or at 0 from the start is the LSB, and n from the start is the msb.
Address + 0 |
Address + 1 |
LSB | MSB |
'b' | 'a' |
- od --endian=big -x letters.dat
- Will switch the byte ordering for a big-endian presentation.
Address + 0 |
Address + 1 |
LSB | MSB |
'a' | 'b' |
- little endian reads small left to right
- Big endian reads big right to left.
- And this is somewhat backwards from our nature.
-
(wikipedia).
- I have some code to examine this.
- By default c++ writes in ascii (ie convert to ascii and write the value)
- If I open a stream with
ifstream inFile(fileName, ios::binary);
it will read binary not ascii.
- I need to do this with
inFile.read(char * location, size_t size);
- It will read in size characters and store them starting at location.
-
inFile.clear()
will remove the eof flag.
-
inFile.seekg(0,ios::beg)
will reset the file pointer to the beginning of the file.
-
a << b
shifts a to the left b times.
- This is equivelent to multiplying by pow(2,b)
- reader.cpp.
- writeShort.cpp is a program that produces data.
- Note, we get the same character output
- Reading in shorts, we get a transposition, ie C++ knows what to do
- There is another program called hexdump that you might investigate.