-->
Previous Table of Contents Next


Viewing Files in Other Forms

Other commands display the contents of files in different forms. For example, if you want to look at the contents of a binary file, display it with the od command, which stands for octal dump. The od command displays a file in octal notation, or base 8. By using various flags, od can display a file in decimal, ASCII, or hexadecimal (base 16).

Octal, Decimal, and Hexadecimal Notation

Representing binary data is an intriguing problem. If the binary data represents ASCII, you have no problem displaying it (ASCII is, after all, what you expect when you look at most files). If the file is a program, however, the data most likely can’t be represented as ASCII characters. In that case, you have to display it in some numerical form.

The early minicomputers used 12-bit words. Today, of course, the computer world has settled on the 8-bit byte as the standard unit of memory. Although you can represent data in the familiar decimal (base 10) system, the question becomes what to display—a byte, a word, or 32 bits? Displaying a given number of bits compactly requires that base 2 be raised to the required number of bits. With the old 12-bit systems, you could represent all 12 bits with four numbers (represented by 23, which was the octal or base 8 format). Because early UNIX systems ran on these kinds of minicomputers, much of the UNIX—and, thus, Linux—notation is in octal. Any byte can be represented by a three-digit octal code that looks like this (this example represents the decimal value of 8):


\010

Because the world has settled on an 8-bit byte, octal is no longer an efficient way to represent data. Hexadecimal (base 16 or 24 ) is a better way. An 8-bit byte can be represented by two hexadecimal digits; a byte whose decimal value is 10 is represented as 0A in hexadecimal.

The od command lets you choose how to display binary data. The general form of the command is one of the following:


od [option]… [file]…

or


od —traditional [file] [[+]offset [[+]label]]

Table 17.3 summarizes the flags you can use with od.

Table 17.3 The od Command Flags

Short Flag Full Flag Description

-A --address-radix=radix Determines how file offsets are printed
-N --read-bytes=bytes Limits dump to bytes input bytes per file
-j --skip-bytes=bytes Skips bytes input bytes first on each file
-s --strings[=bytes] Outputs strings of at least bytes graphic characters
-t --format=type Selects output format or formats
-v --output-duplicates Prevents use of * to mark line suppression
-w --width[=bytes] Outputs bytes bytes per output line
--traditional Accepts arguments in pre-POSIX form
--help Displays this help and exits
--version Outputs version information and exits

The pre-POSIX format specifications in Table 17.4 may be intermixed with the commands in Table 17.3, in which case their effects accumulate.

Table 17.4 Pre-POSIX Format Specifications for od

Short Flag POSIX Equivalent Description

-a -t a Selects named characters
-b -t oC Selects octal bytes
-c -t c Selects ASCII characters or backslash escapes
-d -t u2 Selects unsigned decimal shorts
-f -t fF Selects floats
-h -t x2 Selects hexadecimal shorts
-I -t d2 Selects decimal shorts
-l -t d4 Selects decimal longs
-o -t o2 Selects octal shorts
-x -t x2 Selects hexadecimal shorts

For older syntax (second-call format), offset means -j offset.label is the pseudoaddress at first byte printed, incremented when the dump is progressing. For offset and label, a 0x or 0X prefix indicates hexadecimal. Suffixes may be . (dot) for octal and may be multiplied by 512. The type parameter is made up of one or more of the specifications listed in Table 17.5.

Table 17.5 Type Parameters

Parameter Description

a Named character
c ASCII character or backslash escape
d[size] Signed decimal, size bytes per integer
f[size] Floating point, size bytes per integer
o[size] Octal, size bytes per integer
u[size] Unsigned decimal, size bytes per integer
x[size] Hexadecimal, size bytes per integer


Previous Table of Contents Next