在bash中进行十六进制到二进制的转换 [英] Hex to Binary conversion in bash

查看:197
本文介绍了在bash中进行十六进制到二进制的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用bash将一系列字节从十六进制转换为bin。
,但我一直得到(看似随机)(standard_in)1:语法错误的答复来自以下代码:

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

我做了类似的事情与dec到bin,罚款:

pre $ 我在{0..120}
do
KEYBIN = $(echo obase = 2; ibase = 10; $ i| bc)
echo $ KEYBIN
done

有没有人有一个想法,为什么它使用十进制,但不是与十六进制?
在我看来,语法几乎是相同的(除非我错过了一些非常困难的事情。)

解决方案

BC对于十六进制值的情况有点敏感,改为大写,它应该工作

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



$ b

输出: c $ c> 11000100
10010111
10010001
10001100
10000101
10000111
11000100
10010000
10001100
10001101
10011010
10000011
10000001


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:

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:

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.)

解决方案

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

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

这篇关于在bash中进行十六进制到二进制的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆