Byte operations

Discussion in 'Command Line' started by Matesax, Feb 6, 2014.

  1. Matesax

    Matesax Member

    Messages:
    56
    Likes Received:
    10
    Trophy Points:
    8
    Hi,
    in BASH script I need to write (append) dword aligned string (that's stored in variable - UTF-8 - aligned by zero char) to one file. And also append two integer variables into this file - with word alignment. (word (2 bytes) + word = dword => I need dword (4 bytes) aligned record) How can I make this in easy way? (dword dword dword dword ... word word dword dword dword ... word word dword ...) (blab lah\0 0xA2B5 0xC4D8 lore m\ ip sum\0 0x1852 ...)
    Thank you.

    Last edited: Feb 7, 2014
  2. Matesax

    Matesax Member

    Messages:
    56
    Likes Received:
    10
    Trophy Points:
    8
    Best Answer
    Yes! :D
    Code:
    writeIntegers()
    {
        perl <<END
        open FS, '>>', "$temp/FileTable" or die "Unable to open: $!";
        print FS pack('v', $1), pack('v', $2);
        close FS;
    END
    }
    
    ...
    
    perl <<END
        open FS, '+<', "$fs" or die "Unable to open: $!";
        seek FS, 689, 0;
        print FS pack('s<', $total), pack('s<', $count);
        close FS;
    END
    Last edited: Feb 8, 2014
    DevynCJohnson likes this.
  3. ryanvade

    ryanvade Administrator Staff Member Staff Writer

    Messages:
    1,226
    Likes Received:
    415
    Trophy Points:
    83
  4. Matesax

    Matesax Member

    Messages:
    56
    Likes Received:
    10
    Trophy Points:
    8
    OK - I have:
    Code:
    printf %.4x numberHere | xxd -r -p >> FileTable
    BUT - how can I write in Little Endian - only manualy? (AA55 as 55AA)
    ($((43605 & 255)) and $((43605 >> 8)) looks very horrible)
    Thank you.

    OK - result:
    Code:
    writeInteger()
    {
        printf %.4x $(((($1 & 255) << 8) | ($1 >> 8))) | xxd -r -p >> $temp/FileTable
    }
    
    ...
    
        writeInteger $total
        writeInteger $count
    
        fileName=`basename $1`
        echo -n $fileName >> $temp/FileTable
        align=$((`echo -n $fileName | wc -c` % 4))
    
        if [ $align -gt 0 ]
        then
            truncate -s +$((4 - $align)) $temp/FileTable
        fi
    It works, but I think, that it can be better...

    Simple output:
    Code:
    06 00 04 00 65 64 69 74 2E 62 69 6E 0A 00 03 00 66 69 6C 65 6D 61 6E 2E 62 69 6E 00 0D 00 08 00 68 61 6E 67 6D 61 6E 2E 62 69 6E 00 15 00 02 00 6B 65 79 62 6F 61 72 64 2E 62 69 6E 17 00 02 00 6D 6F 6E 69 74 6F 72 2E 62 69 6E 00 19 00 01 00 73 65 72 69 61 6C 2E 62 69 6E 00 00 1A 00 02 00 76 69 65 77 65 72 2E 62 69 6E 00 00 1C 00 AA 03 74 65 73 74 2E 62 6D 70
    Last edited: Feb 7, 2014
  5. DevynCJohnson

    DevynCJohnson Well-Known Member Staff Member Staff Writer

    Messages:
    1,293
    Likes Received:
    1,032
    Trophy Points:
    113
    I do not know Perl, but it may offer better commands for this task.
    Haider92 likes this.
  6. Matesax

    Matesax Member

    Messages:
    56
    Likes Received:
    10
    Trophy Points:
    8
    Best Answer
    Yes! :D
    Code:
    writeIntegers()
    {
        perl <<END
        open FS, '>>', "$temp/FileTable" or die "Unable to open: $!";
        print FS pack('v', $1), pack('v', $2);
        close FS;
    END
    }
    
    ...
    
    perl <<END
        open FS, '+<', "$fs" or die "Unable to open: $!";
        seek FS, 689, 0;
        print FS pack('s<', $total), pack('s<', $count);
        close FS;
    END
    Last edited: Feb 8, 2014
    DevynCJohnson likes this.

Share This Page