Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 显示文件的十六进制数_Bash_File Io_Hex - Fatal编程技术网

Bash 显示文件的十六进制数

Bash 显示文件的十六进制数,bash,file-io,hex,Bash,File Io,Hex,我想构建一个bash程序,它可以像“hex”编辑器一样读取文件,比如*.bin并打印所有十六进制数。我可以从哪里开始?使用od命令,od-t x1 filename您可以使用od。“od-x文件”为什么要重新发明这个轮子?如果你有hextump,你也可以使用它 od -t x1 filename hexdump -x /usr/bin/binaryfile 编辑:添加了“ByTestStream”功能。如果脚本名称包含单词“stream”(例如,它是一个符号链接,例如ln-s bash he

我想构建一个bash程序,它可以像“hex”编辑器一样读取文件,比如*.bin并打印所有十六进制数。我可以从哪里开始?

使用od命令,od-t x1 filename

您可以使用od。“od-x文件”为什么要重新发明这个轮子?

如果你有hextump,你也可以使用它

od -t x1 filename
hexdump -x /usr/bin/binaryfile

编辑:添加了“ByTestStream”功能。如果脚本名称包含单词“stream”(例如,它是一个符号链接,例如
ln-s bash hextump bash hextump stream
,并作为
/bash hextump stream
)运行),它将输出表示文件内容的连续十六进制字符流。否则,其输出将类似于
hextump-C

因为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 "$1" ]]
then
    exec 3<&0                           # read stdin
    [[ -p /dev/stdin ]] || tty="yes"    # no pipe
else
    exec 3<"$1"            # read file
fi

# if the script name contains "stream" then output will be continuous hex digits
# like hexdump -ve '1/1 "%.2x"'
[[ $0 =~ 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"

od
是Linux程序还是bash函数?抱歉,我真的是一个初学者。@Nathan Campos:你可以通过
找出哪个od
,如果你知道一个程序的名称,那么它就是一个外部程序(对于
od
,它可能是)。Unix非常标准,从一开始就存在了。@GregHewgill这是错误的。从我的Ubuntu机器上看这个。~$类型[[是一个shell内置的~$,它[/usr/bin/[@NathanCampos更重要的是,它是POSIX::-)顺便说一句,这个脚本的输出格式与
hextump-C
hd
相同。很高兴看到有人用bash做不寻常甚至是二进制的事情。非常有趣的一段代码!@nkvnkv:阅读问题:“我想建立一个bash程序…”另外,请注意我的答案被OP接受,并阅读我的答案所附的评论。谢谢你的否决票。 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.........|