Results 1 to 3 of 3

Thread: Unix gurus: Writing in HEX

  1. #1
    Member
    Join Date
    Jan 2008
    Posts
    391
    Thanks
    27
    Thanked 98 Times in 72 Posts
    Rep Power
    242
    Reputation
    1584

    Default Unix gurus: Writing in HEX

    Hey all,

    I am needing to write the size of a file to another file using HEX

    ie: Image0.img has a size of 52013bytes
    Image0.size will contain 00 00 CB 2D when opened using a hex editor. (needs to be 4 bytes in length)

    I can get the hex value saved as a ascii file but not in hex

    Hope it makes sense...

    Cheers



Look Here ->
  • #2
    Junior Member
    Join Date
    Jan 2008
    Location
    IDA
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    198
    Reputation
    10

    Default

    not the best way to do it im sure, there would be a better way using 'pack' or 'unpack' but i managed to get this way working first. It probably would also be easy to use a small c program to make a command line tool to do this too. This one is in perl. It gets the file size, converts it to ascii/hex, splits that in to 2 char/1 byte fragments, then saves them out to disk.

    test file:

    [ant@media ~]$ ls -l a98a_1.JPG
    -rw-rw-r-- 1 ant ant 16939 2008-02-17 21:24 a98a_1.JPG

    running:

    [ant@media ~]$ ./file.pl a98a_1.JPG test.bin
    0000422B
    frag>00<
    frag>00<
    frag>42<
    frag>2B<

    Code:
    [ant@media ~]$ hexdump test.bin 
      Address  | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | 0123456789ABCDEF
    -----------+-------------------------------------------------+-----------------
    0x00000000 | 00 00 42 2B                                     | ..B+
    I was gonna put the code here in a code block, but it looks like the forum eats a bunch of punctuation and variable names, so here it is at megaupload instead.


  • #3
    Member
    Join Date
    Jan 2008
    Posts
    391
    Thanks
    27
    Thanked 98 Times in 72 Posts
    Rep Power
    242
    Reputation
    1584

    Default

    Thanks antus,

    It took a while, but I fiured it out last night... Usual I guess...

    Code:
    ####
    # Create size File
    #
    #HEXSIZE=$(printf "%08Xn" )
    #echo -n  | xxd -r -p | dd obs=4 of="image/Image.size" bs=1 conv=notrunc
    HEXSIZE=$(printf "%08Xn" )
    echo -n  | xxd -r -p | dd count=2 obs=4 of="image/Image.size1" bs=1 conv=notrunc,swab
    echo -n  | xxd -r -p | dd skip=2 count=2 obs=4 of="image/Image.size2" bs=1 conv=notrunc,swab
    cat image/Image.size2 image/Image.size1 > image/Image.size
    rm -f image/Image.size1 image/Image.size2
    I also needed to invert the character so hence the reason for the two echo commands and it's the only method I could get to work....

    Thanks again though antus
    Last edited by Gitch; 14-06-09 at 06:21 PM. Reason: Damn wrong button!! hehe

  • Similar Threads

    1. Gmask - any gurus here?
      By BCNZ in forum PC Software
      Replies: 2
      Last Post: 06-04-09, 10:02 AM
    2. At the third stroke, the Unix time will be 1234567890
      By Globe in forum Operating Systems
      Replies: 0
      Last Post: 14-02-09, 11:37 AM
    3. Attention TV antenna Gurus... HELP!
      By biggeorge in forum Digital Terrestrial Television
      Replies: 13
      Last Post: 26-08-08, 02:20 AM
    4. Mounting USB in unix???
      By morroman in forum Operating Systems
      Replies: 1
      Last Post: 09-07-08, 08:45 AM

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •