using bash: write bit representation of integer to file

前端 未结 9 922
春和景丽
春和景丽 2020-12-15 20:30

I have a file with binary data and I need to replace a few bytes in a certain position. I\'ve come up with the following to direct bash to the offset and show me that it fou

相关标签:
9条回答
  • 2020-12-15 21:11

    In my case, I needed to go from a decimal numeric argument to the actual unsigned 16-bit big endian value. This is probably not the most efficient way, but it works:

    # $1 is whatever number (0 to 65535) the caller specifies
    DECVAL=$1
    HEXSTR=`printf "%04x" "$DECVAL"`
    BYTEONE=`echo -n "$HEXSTR" | cut -c 1-2`
    BYTETWO=`echo -n "$HEXSTR" | cut -c 3-4`
    echo -ne "\x$BYTEONE\x$BYTETWO" | dd of="$FILENAME" bs=1 seek=$((0xdeadbeef)) conv=notrunc
    
    0 讨论(0)
  • 2020-12-15 21:12

    Worked like a treat. I used the following code to replace 4 bytes at byte 24 in little endian with two integers (1032 and 1920). The code does not truncate the file.

    echo -e \\x08\\x04\\x80\\x07 | dd of=<file> obs=1 oseek=24 conv=block,notrunc cbs=4
    

    Thanks again.

    0 讨论(0)
  • 2020-12-15 21:12

    You might put the desired input into a file and use the "if=" option to dd to insert exactly the input you desire.

    0 讨论(0)
提交回复
热议问题