bash 显示文件的十六进制数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2003803/
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
Show Hexadecimal Numbers Of a File
提问by Nathan Campos
I want to build a bash program that can read a file, like a *.bin and print all its hexadecimal numbers, as 'hex' editors do. Where I can start?
我想构建一个 bash 程序,它可以读取一个文件,比如 *.bin 并打印它的所有十六进制数字,就像 'hex' 编辑器所做的那样。我可以从哪里开始?
回答by President James K. Polk
Use the od command,
使用 od 命令,
od -t x1 filename
回答by Paused until further notice.
Edit:Added "bytestream" functionality. If the script name contains the word "stream" (e.g. it's a symlink such as ln -s bash-hexdump bash-hexdump-streamand run as ./bash-hexdump-stream), it will output a continuous stream of hex characters representing the contents of the file. Otherwise its output will look like hexdump -C.
编辑:添加了“字节流”功能。如果脚本名称包含单词“stream”(例如,它是一个符号链接,例如ln -s bash-hexdump bash-hexdump-stream和 run as ./bash-hexdump-stream),它将输出一个连续的十六进制字符流,代表文件的内容。否则它的输出看起来像hexdump -C.
It takes a bunch of trickery since Bash isn't really good at binary:
由于 Bash 并不擅长二进制,因此需要一些技巧:
#!/bin/bash
# bash-hexdump
# by Dennis Williamson - 2010-01-04
# in response to http://stackoverflow.com/questions/2003803/show-hexadecimal-numbers-of-a-file
# usage: bash-hexdump file
if [[ -z "" ]]
then
exec 3<&0 # read stdin
[[ -p /dev/stdin ]] || tty="yes" # no pipe
else
exec 3<"" # read file
fi
# if the script name contains "stream" then output will be continuous hex digits
# like hexdump -ve '1/1 "%.2x"'
[[ 00000000 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
00000010 02 00 03 00 01 00 00 00 e0 1e 06 08 34 00 00 00 |............4...|
00000020 c4 57 0d 00 00 00 00 00 34 00 20 00 09 00 28 00 |.W......4. ...(.|
00000030 1d 00 1c 00 06 00 00 00 34 00 00 00 34 80 04 08 |........4...4...|
. . .
00000150 01 00 00 00 2f 6c 69 62 2f 6c 64 2d 6c 69 6e 75 |..../lib/ld-linu|
00000160 78 2e 73 6f 2e 32 00 00 04 00 00 00 10 00 00 00 |x.so.2..........|
00000170 01 00 00 00 47 4e 55 00 00 00 00 00 02 00 00 00 |....GNU.........|
=~ stream ]] && nostream=false || nostream=true
saveIFS="$IFS"
IFS="" # disables interpretation of \t, \n and space
saveLANG="$LANG"
LANG=C # allows characters > 0x7F
bytecount=0
valcount=0
$nostream && printf "%08x " $bytecount
while read -s -u 3 -d '' -r -n 1 char # -d '' allows newlines, -r allows \
do
((bytecount++))
printf -v val "%02x" "'$char" # see below for the ' trick
[[ "$tty" == "yes" && "$val" == "04" ]] && break # exit on ^D
echo -n "$val"
$nostream && echo -n " "
((valcount++))
if [[ "$val" < 20 || "$val" > 7e ]]
then
string+="." # show unprintable characters as a dot
else
string+=$char
fi
if $nostream && (( bytecount % 8 == 0 )) # add a space down the middle
then
echo -n " "
fi
if (( bytecount % 16 == 0 )) # print 16 values per line
then
$nostream && echo "|$string|"
string=''
valcount=0
$nostream && printf "%08x " $bytecount
fi
done
if [[ "$string" != "" ]] # if the last line wasn't full, pad it out
then
length=${#string}
if (( length > 7 ))
then
((length--))
fi
(( length += (16 - valcount) * 3 + 4))
$nostream && printf "%${length}s\n" "|$string|"
$nostream && printf "%08x " $bytecount
fi
$nostream && echo
LANG="$saveLANG";
IFS="$saveIFS"
The apostrophe trick is documented here. The relevant part says:
撇号技巧记录在这里。相关部分说:
If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote.
如果前导字符是单引号或双引号,则该值应为跟在单引号或双引号之后的字符的底层代码集中的数值。
Here is some output from the script showing the first few lines of my /bin/bashplus a few more:
这是脚本的一些输出,显示了我的前几行/bin/bash加上更多:
hexdump -x /usr/bin/binaryfile
回答by Richard Pennington
You could use od. "od -x file" Why reinvent that wheel?
你可以使用 od。“od -x 文件” 为什么要重新发明轮子?
回答by ghostdog74
you can also use hexdump if you have it
如果你有它,你也可以使用 hexdump
##代码##
