[prog] bash, reading and displaying hex

Hamster hamster at hamsternet.org
Sun Nov 9 20:21:46 EST 2003


Hi,

I'm trying to write a bash script to read the Primary Volume Descriptor
of a CD (using dd) and have run into problems when it comes to how it deals
with hex. I'll try to explain.

On a cd, the 33581st, 33582nd, 33583th and 33584th bytes contain the year in
which the cd was burnt. So if you run the command:

dd if=/dev/cdrom bs=1 skip=33581 count=4

It will print 2003 (or whatever the year is). If you redirect the output of
this command to a file, and then use a hexeditor to view it, you'll see that
the year is stored as 32 30 30 33, the ascii values for 2 0 0 3

So in a bash script there is no problem doing something like:

year=`dd if=/dev/cdrom bs=1 skip=33581 count=4`
echo $year

to make it spit out 2003.

No problems so far.

However. 

The 33597th byte (which is part of the same date/time entry as the above
example) stores the Greenwich Mean Time Offset in increments of 15 minutes.

If you do:

dd if=/dev/cdrom skip=33597 count=1 >somefile

and use a hexeditor to view somefile, you might see an entry like 0x08,
which means a GMT offset of 8x15 mins = 2 hours.

Here's the problem.

Because the info in this byte is not an ascii character, if you let dd print
its output to STDOUT, it doesnt print anything - 0x08 is actually the ascii
backspace character. Neither can you assign the output of this dd command to
a variable.

So here's my question. Is there anyway to get my bash script to read this
byte and print it out?? I wanted to do something like:

offset=`dd if=/dev/cdrom skip=33597 count=1`
printf 'The GMT offset is 0x%02X' $offset

only this of course doesn't work.

Does anyone know of a way in which I can get the variable "offset" to
contain the hex value that dd reads?? Or is this something bash
cant do?

I hope I've explained the problem well enough...

Thanks

Hamster


More information about the Programming mailing list