Linux shell scripting: hex number to binary string
I used 'bc' command in Linux. (much more complex calculator than converting!)
echo 'ibase=16;obase=2;5f' | bc
ibase parameter is the input base (hexa in this case), and obase the output base (binary).
Hope it helps.
Hex to Binary conversion in bash
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
Convert a decimal number to hexadecimal and binary in a shell script
The following one-liner should work:
printf "%s %08d 0x%02x\n" "$1" $(bc <<< "ibase=10;obase=2;$1") "$1"
Example output:
$ for i in {1..10}; do printf "%s %08d 0x%02x\n" "$i" $(bc <<< "ibase=10;obase=2;$i") "$i"; done
1 00000001 0x01
2 00000010 0x02
3 00000011 0x03
4 00000100 0x04
5 00000101 0x05
6 00000110 0x06
7 00000111 0x07
8 00001000 0x08
9 00001001 0x09
10 00001010 0x0a
Convert binary data to hexadecimal in a shell script
Perhaps use xxd
:
% xxd -l 16 -p /dev/random
193f6c54814f0576bc27d51ab39081dc
How to convert Hexadecimal to Decimal in bash/scripting by only using functions
I was always told you convert a number from one base to another by converting single digit from the back in that number and then multiply that number by the base and position it is in.
First I wrote a function to convert a single digit from hex to dec. Then I convert the number from the most significant digit (from the one to the left) to dec, then multiply by 16, then add the next digit, then mutliply by 16 and so on.
hextodec1() {
case "$1" in
[0-9]) echo "$1"; ;;
a|A) echo 10; ;;
b|B) echo 11; ;;
c|C) echo 12; ;;
d|D) echo 13; ;;
e|E) echo 14; ;;
f|F) echo 15; ;;
esac
}
out=0
for ((i = ${#num} - 1; i >= 0; --i)); do
((out = out * 16 + $(hextodec1 ${num:$i:1}) ))
done
echo $out
You can extract a substring of string by using ${variable_name:$start_position:$length}
.
But really the simplest way is to do this:
echo $((0x$num))
You algorithm needs some fixing because bash understands decimal numbers by default, or hexadecimal numbers if pretended with 0x
. So you loop for (( x=0; num>0; x++ )); do
will not work for num=6C3
cause num is a string, and bash can't compare number x
to string 6C3
. You can do for ((x=0;(0x$num)>0;++x)); do
but then you might better just echo $((0x$num))
, cause the first time you will num=$(( (0x$num) % 2 ))
it will be converted to decimal and the next time bash enters the loop condition the variable will be in decimal not hex.
BASh - Convert list of hex values to binary file (application)
Simply, use xxd directly from a bash like
xxd outfile > outfile.hex
and you will see, here isn't any 0a
.
The 0a
is appended somewhere when the vim sends a line to xxd command. If you want convert inside vim - try use
vim -b outfile
what open the outfile
in binary mode.
Related Topics
List Files That Are in Directory1 But Not in Directory2 and Vice Versa
How to Flush Stdout of a Running Process
Where Is The Linux Socket Implementation
Where Do You Send The Kernel Console on an Embedded System
How to Run Sh File from Another Sh File
Undelete The Deleted Command in Bash
Install Python 32 Bit on 64 Bit Linux
Docker Can't Write to Directory Mounted Using -V Unless It Has 777 Permissions
How to Play an Audio File from Haskell Code, Cross-Platform
See What Process Last Touched a File
Detect When Reader Closes Named Pipe (Fifo)
Do I Have to Pthread_Join Each Thread I Create
Controlling The Boot Screen on Linux Embedded App
Goroutine in Io Wait State for Long Time
How to Type "Cargo Run" Without Needing to Set The Ld_Library_Path Shell Variable