bash 十六进制到二进制转换器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10689221/
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
Hex To Binary Converter
提问by KarlsD
How I can convert in bash a string in hex (for example "d43c23F1") to binary.
如何在 bash 中将十六进制字符串(例如“d43c23F1”)转换为二进制。
Without using the utility bc.
不使用实用程序 bc。
Hex:Binary
十六进制:二进制
d43c23F1:11010100001111000010001111110001
d43c23F1:11010100001111000010001111110001
回答by Emil Vikstr?m
Every hex digit corresponds to exactly four binary digits. Make a dictionary of those mappings and loop over every hex character, appending the four digits to the answer. I'm not posting code since I haven't that much time, but you should be able to proceed from here.
每个十六进制数字正好对应四个二进制数字。制作这些映射的字典并遍历每个十六进制字符,将四位数字附加到答案中。我没有发布代码,因为我没有那么多时间,但你应该可以从这里开始。
回答by larsks
Can you use dcinstead? Then you can do:
可以dc代替吗?然后你可以这样做:
$ echo 16 i 2 o D43C23F1 p | dc
11010100001111000010001111110001
Otherwise, you could follow Emil's suggestion using bash "associative arrays". There is a Linux Journal articlethat talks about them in some detail. Create a dictionary that maps hex digits to their binary representation:
否则,您可以按照 Emil 的建议使用 bash“关联数组”。有一篇Linux Journal 文章详细讨论了它们。创建一个将十六进制数字映射到其二进制表示的字典:
declare -A bits
bits[A]=1010
Split your hex string into digits, then look each one up in the array.
将您的十六进制字符串拆分为数字,然后在数组中查找每个数字。
回答by Spencer Rathbun
Shamelessly, stolen from here, with modifications for hex.
#!/bin/bash
# SCRIPT: hex2binary.sh
# USAGE: hex2binary.sh Hex_Number(s)
# PURPOSE: Hex to Binary Conversion. Takes input as command line
# arguments.
# \\ ////
# \ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# 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
You must start your hex number with 0xfor this to work.
您必须以十六进制数字开头0x才能使其正常工作。
回答by Merson Su
Try this:
尝试这个:
$ echo "ibase=16; obase=2; D43C23F1" | bc
回答by technosaurus
this code assumes your hex is in caps (necessary for other bases that may use capital and lower case) - just add the functions to your bashrc
此代码假定您的十六进制是大写的(对于可能使用大写和小写的其他基础是必需的)-只需将函数添加到您的 bashrc
toupper() { #change input $@ to upper case
uppers=ABCDEFGHIJKLMNOPQRSTUVWXYZ
lowers=abcdefghijklmnopqrstuvwxyz
i=0
while ([ $i -lt ${#1} ]) do
CUR=${1:$i:1}
case $lowers in
*$CUR*)CUR=${lowers%$CUR*};OUTPUT="${OUTPUT}${uppers:${#CUR}:1}";;
*)OUTPUT="${OUTPUT}$CUR";;
esac
i=$((i+1))
done
printf "${OUTPUT}"
}
baseN2dec(){ #usage: baseN2dec number <base> (default base is 16)
#note base32 and base64 should really have a different string pattern ... todo
#RFC 4648 Base32 alphabet ABCDEFGHIJKLMNOPQRSTUVWXYZ234567 we use base32hex
#Base 64 is ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
STRING=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
i=$((${#1}-1));DEC=0;POS=1;BASE=${2:-16}
while ([ $i -ge 0 ]) do
VAL=${1:$i:1}
VAL=${STRING%${VAL}*}
VAL=${#VAL}
DEC=$(($VAL*$POS+$DEC))
POS=$(($BASE*$POS))
i=$(($i-1))
done
echo $DEC
}
dec2baseN(){ #usage: dec2baseN number <base>
A=$((/))
STRING=0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/
[ $A -ge ] && dec2baseN ${A} || printf ${STRING:$A:1}
printf ${STRING:$((%)):1}
}
dec2bin(){
dec2baseN 2
}
dec2hex(){
dec2baseN 16
}

