convert hex to binary and send to variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert hex to binary and send to variable
# 1  
Old 12-01-2010
convert hex to binary and send to variable

Folks,
can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated.

Thanks
venu

Its in korn shell......

Last edited by venu; 12-01-2010 at 11:15 PM..
# 2  
Old 12-01-2010
I think the first question is what scripting language do you wish to use?
This User Gave Thanks to fpmurphy For This Post:
# 3  
Old 12-01-2010
simple example using bc. parse the output into whatever you want

Code:
$ bc <<EOF
> ibase=16
> obase=2
> FFFF
> EOF
1111111111111111

This User Gave Thanks to frank_rizzo For This Post:
# 4  
Old 12-01-2010
Code:
$ cat hex2bin.awk

BEGIN {
FS=""
a["f"]="1111"
a["e"]="1110"
a["d"]="1101"
a["c"]="1100"
a["b"]="1011"
a["a"]="1010"
a["9"]="1001"
a["8"]="1000"
a["7"]="0111"
a["6"]="0110"
a["5"]="0101"
a["4"]="0100"
a["3"]="0011"
a["2"]="0010"
a["1"]="0001"
a["0"]="0000"
}
{
for (i=1;i<=NF;i++) printf a[tolower($i)]" "
print ""
}

$ set -- $(echo 40e3 |awk -f hex2bin.awk)

$ echo $1
0100

$ echo $2
0000

$ echo $3
1110

$ echo $4
0011

After run above set command, you will get var $1,$2, $3,,, $8 directly.

Last edited by rdcwayx; 12-02-2010 at 02:14 AM.. Reason: a[tolower($i)]
This User Gave Thanks to rdcwayx For This Post:
# 5  
Old 12-01-2010
If you have gawk and bash:

Code:
$ cat bits.awk
#!/bin/bash
echo $((16#$1)) | gawk ' {
for(i=lshift(1,31);i;i=rshift(i,1))
printf "%d%s", (and($1,i) > 0), ++c%4==0?" ":"";
printf"\n"; }'
 
$ ./bits.awk AFC92317
1010 1111 1100 1001 0010 0011 0001 0111
 
$ set -- $(./bits.awk AFC92317)
 
$ echo $4
1001

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 12-02-2010
If your Korn shell is a recent version of ksh93, try the following
Code:
#!/bin/ksh93

integer -i2 mask=16#F0000000
integer -i2 v0 v1 v2 v3 v4 v5 v6 v7

read hn?"Enter a 32-bit hex decimal number: "
integer -i2 in=16#$hn

for ((i=0; i < 8; i++))
do
   nameref var=v${i}
   var=$(( ((in << (i*4)) & mask) >> 28 ))
done

printf "%032.0.2d\n" $in
printf "%04.0.2d %04.0.2d %04.0.2d %04.0.2d %04.0.2d %04.0.2d %04.0.2d %04.0.2d\n" \
   $v0 $v1 $v2 $v3 $v4 $v5 $v6 $v7

Here is an example of the output:
Code:
Enter a 32-bit hex decimal number: AFC92317
10101111110010010010001100010111
1010 1111 1100 1001 0010 0011 0001 0111

This User Gave Thanks to fpmurphy For This Post:
# 7  
Old 12-02-2010
OK if you have gawk and ksh:

Code:
$ cat bits.awk
#!/bin/ksh
echo $1 | gawk --non-decimal-data ' {
for(i=lshift(1,31);i;i=rshift(i,1))
printf "%d%s", (and(("0x"$1)+0,i) > 0), ++c%4==0?" ":"";
printf"\n"; }'
 
$ ./bits.awk BADBEEF
0000 1011 1010 1101 1011 1110 1110 1111
 
$ set -- $(./bits.awk BADBEEF)
 
$ echo $4
1101

---------- Post updated at 04:20 PM ---------- Previous update was at 03:56 PM ----------

Using bc and standard awk

Code:
echo "ibase=16 ; obase=2 ; $1" | bc | awk '{
          A=substr("00000000000000000000000000000000",1,32-length($0))$0;
          for(i=1;i<=32;i++)
               printf "%s%s", substr(A,i,1), i%4?"":" ";
           printf "\n"}'

---------- Post updated at 04:33 PM ---------- Previous update was at 04:20 PM ----------

or with bc, standard awk and sed:

Code:
echo "ibase=16 ; obase=2 ; $1" | bc | awk '{
     print substr("00000000000000000000000000000000",1,32-length($0))$0; }' | sed 's/..../& /g'


Last edited by Chubler_XL; 12-02-2010 at 05:19 AM..
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Binary File To Hex In Linux

dHi, I have the attached file(actual file can be extracted post unzipping it) & i am trying to use the following code for coversion to hex format. Starting hex value is 84 which is start of the record & termination is done using 00 00 followed by 84(hex) which i can see in the dump clearly using... (14 Replies)
Discussion started by: siramitsharma
14 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. Shell Programming and Scripting

Convert hex to decimal

can someone help me in converting hex streams to decimal values using perl script Hex value: $my_hex_stream="0c07ac14001676"; Every hex value in the above stream should be converted in to decimal and separated by comma. The output should be: 12,07,172,20,00,22,118 (2 Replies)
Discussion started by: Arun_Linux
2 Replies

4. Shell Programming and Scripting

Convert Hex - KSH

Hello, I woild like to convert hex on KSH not BASH: I tried to use: tmp=31 printf "\x"${tmp}"" it works on bash - Output is '1' but not on ksh. please advice on the right syntax. Thanks. (4 Replies)
Discussion started by: LiorAmitai
4 Replies

5. Programming

Convert HEX to EBCDIC IN C

i want to convert Hex value To EBCDIC value. i tried to convert hex to ascii and then to ebcdic but it doesn't give desired results . it doesn't give corresponding ebcdic value instead it gives some junk values. e.g; Hex EBCDIC ----------------- 81 a 82 b 83 c 84 d 85 e 86 f 87... (6 Replies)
Discussion started by: junaid.nehvi
6 Replies

6. Shell Programming and Scripting

binary/hex output

i want to output something like 2f 00 00 00 but i can't seem to escape characters like i'm used to in some programming languages, so with this: echo "/\0\0\0" >> outputfile i actually get 2f 5c 30 5c 30 5c 30 0a ie the \0 isn't giving me the 00 i need, and in addition it has got an... (8 Replies)
Discussion started by: peterworth
8 Replies

7. Shell Programming and Scripting

How to Convert Hex value to Dec ?

Hi All, I want to convert below Hex value to Dec value in each column .How to do it ? This data is in a 1 file. 4e20 0475 2710 010f 7530 69a2 7530 7e2f 4e20 02dd 7530 6299 4e20 0c0a 7530 69a2 4e20 0a0b 2710 0048 7530 7955 4e20 0d23 7530 622d 7530 9121 2710 001f 7530 7d3f (6 Replies)
Discussion started by: Nayanajith
6 Replies

8. Programming

to convert int to hex

Hi, Can you help me in converting int value to hex in a single command. Thanks (8 Replies)
Discussion started by: naan
8 Replies

9. UNIX for Advanced & Expert Users

Modifying binary file by editing Hex values ?

Hi , i'm using special binary file (lotus notes) and modifying an hexadecimal address range with windows hex editor and it works fine ! The file is an AIX one and i'm forced to transfert (ftp) it before modifying it and re-transfert ! NOW i would do this directly under AIX ! I can... (4 Replies)
Discussion started by: Nicol
4 Replies

10. Shell Programming and Scripting

Binary and hex in unix

not much familiar with binary and hex calculation in script programming.... explaination: binary format control the parameter turned on or off in the program stored in hex mode, the question is: how to change 39e to 19e using the binary calculation(although i don't know the command for... (2 Replies)
Discussion started by: trynew
2 Replies
Login or Register to Ask a Question