using bash: write bit representation of integer to file

前端 未结 9 940
春和景丽
春和景丽 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
    

提交回复
热议问题