将十进制值作为两个小端十六进制字节输出到文件
Outputting a decimal value as two little-endian hex bytes to a file
我需要执行以下操作的 bash 命令:
Write a new file in which the two first bytes contain the raw hex bytes (not ASCII) of a value (start address in this case). I.e., 49152 should be written to the file as 00 c0
.
我尝试了printf
、xxd
、fold
、tac/cat
等几种组合
printf "%X\n" 49152 | fold -w2|tac|tr -d "\n"
这向我提供了我需要的 ASCII 表示形式,而不是原始的 HEX 字节。
value=49152
printf "\x$(printf %x $(($value-$value/256*256)))\x$(printf %x $(($value/256)))" > file.txt
hexdump -C file.txt
输出:
00000000 00 c0 |..|
00000002
我需要执行以下操作的 bash 命令:
Write a new file in which the two first bytes contain the raw hex bytes (not ASCII) of a value (start address in this case). I.e., 49152 should be written to the file as
00 c0
.
我尝试了printf
、xxd
、fold
、tac/cat
等几种组合
printf "%X\n" 49152 | fold -w2|tac|tr -d "\n"
这向我提供了我需要的 ASCII 表示形式,而不是原始的 HEX 字节。
value=49152
printf "\x$(printf %x $(($value-$value/256*256)))\x$(printf %x $(($value/256)))" > file.txt
hexdump -C file.txt
输出:
00000000 00 c0 |..| 00000002