How to convert 'long' to four bytes in Bourne shell

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iamef
    New Member
    • Sep 2011
    • 1

    How to convert 'long' to four bytes in Bourne shell

    Dear All,

    Is there a way to convert long decimal int value to 4-bytes, using Bourne Shell or any commands from minimal Linux (sed, awk, etc)?
    The goal is to calculate size of file and place the 4-byte value before it. I.e:

    #!/bin/sh
    size=`du filename`
    cat <convert $size to 4 bytes> filename >filename

    Appreciate your help.

    UPD: i have found a part of solution:

    n = 12345 # sample long int file size
    "let n0 = $n % 256"
    "let n = $n / 256"
    "let n1 = $n % 256"
    "let n = $n / 256"
    "let n2 = $n % 256"
    "let n = $n / 256"
    "let n3 = $n % 256"
    echo $n0 $n1 $n2 $n3

    So now I have these 4 ints representing bytes. The remaining questions is how to write it as 4 bytes to file?
    Last edited by iamef; Sep 9 '11, 12:54 PM. Reason: Found part of solution
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    The size of a long int will depend on several different things.

    I would recommend checking the size of your long ints, it may already be 4 bytes.

    Comment

    Working...