[prog] bash, reading and displaying hex

John Clarke johnc+linuxchix at kirriwa.net
Mon Nov 10 11:05:14 EST 2003


On Sun, Nov 09, 2003 at 07:21:46 +0100, Hamster wrote:

> 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

You could use od:

    [johnc at dropbear ~]$ echo -n ^H>temp
    [johnc at dropbear ~]$ od -t x1 temp
    0000000 08
    0000001

(if anyone's wondering how I entered control-h into the shell, I used
control-v followed by control-h).

Now without the addresses:

    [johnc at dropbear ~]$ od -A n -t x1 temp
    08

And finally assign it to a variable:

    [johnc at dropbear ~]$ offset=`dd if=temp count=1 bs=1|od -A n -t x1`
    [johnc at dropbear ~]$ echo $offset
    08

To include the '0x' prefix, change od's output format to unsigned
decimal and use printf instead of echo:

    [johnc at dropbear ~]$ offset=`dd if=temp count=1 bs=1|od -A n -t u1`
    [johnc at dropbear ~]$ printf "0x%02x\n" $offset
    0x08


Cheers,

John
-- 
whois !JC774-AU at whois.aunic.net
GPG key id: 0xD59C360F
http://kirriwa.net/john/


More information about the Programming mailing list