using bash: write bit representation of integer to file

前端 未结 9 921
春和景丽
春和景丽 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 20:48

    If you're willing to rely on bc (which is fairly common)

    echo -e "ibase=16\n obase=2 \n A1" | bc -q
    

    might help.

    0 讨论(0)
  • 2020-12-15 20:57

    printf is more portable than echo. This function takes a decimal integer and outputs a byte with that value:

    echobyte () {
        if (( $1 >= 0 && $1 <= 255 ))
        then
            printf "\\x$(printf "%x" $1)"
        else
            printf "Invalid value\n" >&2
            return 1
        fi
    }
    
    $ echobyte 97
    a
    $ for i in {0..15}; do echobyte $i; done | hd
    00000000  00 01 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
    00000010
    
    0 讨论(0)
  • 2020-12-15 21:04

    I have a function to do this:

    # number representation from 0 to 255 (one char long)
    function chr() { printf "\\$(printf '%03o' "$1")" ; return 0 ; }
    # from 0 to 65535 (two char long)
    function word_litleendian() { chr $(($1 / 256)) ; chr $(($1 % 256)) ; return 0 ; }
    function word_bigendian() { chr $(($1 % 256)) ; chr $(($1 / 256)) ; return 0 ; }
    # from 0 to 4294967295 (four char long)
    function dword_litleendian() { word_lilteendian $(($1 / 65536)) ; word_litleendian $(($1 % 65536)) ; return 0 ; }
    function dword_bigendian() { word_bigendian $(($1 / 65536)) ; word_bigendian $(($1 % 65536)) ; return 0 ; }
    

    You can use piping or redirection to catch the result.

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

    xxd is the better way. xxd -r infile outfile will take ascii hex-value in infile to patch outfile, and you can specify the specific position in infile by this: 1FE:55AA

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

    You can use echo to emit specific bytes using hex or octal. For example:

    echo -n -e \\x30 
    

    will print ascii 0 (0x30)

    (-n remove trailing newline)

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

    With bash, "printf" has the "-v" option, and all shell has logical operators.

    So here is simplier form in bash :

    int2bin() {
      local i=$1
      local f
      printf -v f '\\x%02x\\x%02x\\x%02x\\x%02x' $((i&255)) $((i >> 8 & 255)) $((i >> 16 & 255)) $((i >> 24 & 255))
      printf "$f"
    }
    
    0 讨论(0)
提交回复
热议问题