如何在 Bash 中写入二进制数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43214001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:58:47  来源:igfitidea点击:

How to write binary data in Bash

bashshell

提问by Jeevansai Jinne

Let us say there is a variable in bash script with value "001" how can i write this binary data into a file as bits (as "001" not "1") echo writes it as string but i want to write in bits.

假设 bash 脚本中有一个值为“001”的变量,我如何将此二进制数据作为位(如“001”而不是“1”)写入文件中,echo 将其写入为字符串,但我想以位写入。

回答by that other guy

You can write arbitrary bytes in hex or octal with:

您可以使用十六进制或八进制写入任意字节:

printf '\x03' > file   # Hex
printf '
printf '%o\n' "$((2#00000011))"
3' > file # Octal

If you have binary, it's a bit tricker, but you can turn it into octal with:

如果你有二进制,它有点棘手,但你可以把它变成八进制:

binary=00000011
printf "\$(printf '%o' "$((2#$binary))")" > file

which of course can be nested in the above:

这当然可以嵌套在上面:

printf "%b" "2"
printf "%b" "\x0a"

Note that this only works with up to 8 bits. If you want to write longer values, you have to split it up into groups of 8.

请注意,这只适用于最多 8 位。如果你想写更长的值,你必须把它分成 8 个一组。

回答by jm666

Just use the %bin the printf:

只需%b在 printf 中使用:

printf "%b" "\x0a" | od -bc

%b - Print the associated argument while interpreting backslash escapes in there

%b - 在解释反斜杠转义时打印相关参数

so, above both prints binary value for the decimal 10.

所以,上面都打印十进制的二进制值10

0000000   012                                                            
          \n      

output

输出

printf "%b" "\xa\n" | od -bc

0000000   012 012 012                                                    
          \n  \n  \n            

you can even mix

你甚至可以混合

# Write 32 bits 5 times to a file 
for i in {1..5}
do
   printf '\x12\x34\xAC\xCD' >> binaryfile.bin
done

回答by Xofo

Most often you want to write a pattern (for instance a 32 bit pattern).

大多数情况下,您想编写一个模式(例如 32 位模式)。

for i in {1..255}
do
    hx=$( printf "%x" $i )
    output="\x$hx\x00\x00\x00"
    printf "%b" $output >> binaryfile.bin 
done

Another example:

另一个例子:

le16 () { # little endian 16 bit;  1st param: integer to 2nd param: file 
  v=`awk -v n= 'BEGIN{printf "%04X", n;}'`
  echo -n -e "\x${v:2:2}\x${v:0:2}" >> 
}

le32 () { # 32 bit version
  v=`awk -v n= 'BEGIN{printf "%08X", n;}'`
  echo -n -e "\x${v:6:2}\x${v:4:2}\x${v:2:2}\x${v:0:2}" >> 
}

回答by Hal Sampson

A couple of more general functions to output integers to a file:

几个将整数输出到文件的更通用的函数:

VALUE="3F"
INPUT_BASE=16
OUTPUT_BASE=2
printf "%b\n" $(bc <<< "ibase=$INPUT_BASE; obase=$OUTPUT_BASE; $VALUE")

回答by Sam

Using bc(in CentOS 7.4):

使用bc(在 CentOS 7.4 中):

VALUE="3F"
INPUT_BASE=16
OUTPUT_BASE=2
printf "%16b\n" $(bc <<< "ibase=$INPUT_BASE; obase=$OUTPUT_BASE; $VALUE") | sed 's^ ^0^g'

RESULT: 111111

结果:111111

If leading zeros are needed then:

如果需要前导零,则:

##代码##

RESULT: 0000000000111111

结果:0000000000111111

Note the use of 16 in the printf format; change value to limit leading-zero count

注意 printf 格式中 16 的使用;更改值以限制前导零计数