bash bash中的十六进制到二进制转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11120324/
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-09 22:16:45  来源:igfitidea点击:

Hex to Binary conversion in bash

bashbinaryhexbc

提问by fragman

I'm trying to convert a series of bytes from hex to bin using bash. but I keep getting (seemingly random) "(standard_in) 1: syntax error" replies from the following code:

我正在尝试使用 bash 将一系列字节从十六进制转换为 bin。但我不断收到(看似随机的)“(standard_in)1:语法错误”来自以下代码的回复:

for j in c4 97 91 8c 85 87 c4 90 8c 8d 9a 83 81
do
        BIN=$(echo "obase=2; ibase=16; $j" | bc )
        echo $BIN
done

I did a similar thing with dec to bin, which works perfectly fine:

我对 dec to bin 做了类似的事情,效果很好:

for i in {0..120}
do
        KEYBIN=$(echo "obase=2; ibase=10; $i" | bc)
        echo $KEYBIN
done

Does anyone have an idea why it works with decimal, but not with hex? In my opinion the syntax is pretty much the same (unless I'm missing something really hard.)

有谁知道为什么它适用于十进制,而不适用于十六进制?在我看来,语法几乎相同(除非我遗漏了一些非常困难的东西。)

回答by Fredrik Pihl

BC is a bit sensitive to case for hex values, change to uppercase and it should work

BC 对十六进制值的大小写有点敏感,更改为大写,它应该可以工作

for j in C4 97 91 8C 85 87 C4 90 8C 8D 9A 83 81
do
        BIN=$(echo "obase=2; ibase=16; $j" | bc )
        echo $BIN
done

Output:

输出:

11000100
10010111
10010001
10001100
10000101
10000111
11000100
10010000
10001100
10001101
10011010
10000011
10000001

回答by Spencer Rathbun

Here's the script I use:

这是我使用的脚本:

#!/bin/bash
# SCRIPT:  hex2binary.sh
# USAGE:   hex2binary.sh Hex_Number(s)
# PURPOSE: Hex to Binary Conversion. Takes input as command line
#          arguments.
#####################################################################
#                      Script Starts Here                           #
#####################################################################

if [ $# -eq 0 ]
then
    echo "Argument(s) not supplied "
    echo "Usage: hex2binary.sh hex_number(s)"
else
    echo -e "3[1mHEX                 \t\t BINARY3[0m"

    while [ $# -ne 0 ]
    do
        DecNum=`printf "%d" `
        Binary=
        Number=$DecNum

        while [ $DecNum -ne 0 ]
        do
            Bit=$(expr $DecNum % 2)
            Binary=$Bit$Binary
            DecNum=$(expr $DecNum / 2)
        done

        echo -e "$Number              \t\t $Binary"
        shift
        # Shifts command line arguments one step.Now  holds second argument
        unset Binary
    done

fi

回答by stu

I came up with this:

我想出了这个:

printf converts to hex, xxd -r -p takes the ascii hex stream and makes it actual binary

printf 转换为十六进制, xxd -r -p 采用 ascii 十六进制流并使其成为实际的二进制

dumping with hexdump to prove it worked...

用 hexdump 转储以证明它有效......

printf "%016x" 53687091200 | xxd -r -p | hexdump -C

回答by zhwsh

Try this:

尝试这个:

for j in c4 97 91 8c 85 87 c4 90 8c 8d 9a 83 81
do
    BIN=$(echo "obase=2; ibase=16; ${j^^}" | bc )
    echo $BIN
done