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
# 15  
Old 12-03-2010
Code:
$ echo ${mask#2#} | awk '{for(i=1;i<=NF;i++)if($i)print i-1}' FS=
0
13
24
30

But shouldn't the position be determined from right to left?
Code:
$ echo ${mask#2#} | awk '{for(i=NF;i>=1;i--)if($i)print NF-i}' FS=
0
6
17
30

Code:
$ P=( echo ${mask#2#} | awk '{for(i=NF;i>=1;i--)if($i)print NF-i}' FS= )
$ echo ${P[0]}
0
$ echo ${P[1]}
6
$ echo ${P[2]}
17
$ echo ${P[3]}
30

This User Gave Thanks to Scrutinizer For This Post:
# 16  
Old 12-03-2010
Oh ! ... i never noticed this before
~ is described as "Bitwise not" in oreilly ksh book ... but it is not the behaviour i see, xor 1 should be used instead

Code:
$ echo $SHELL
/bin/ksh
$ a=$(($a^1)) && echo $a
0
$ a=$(($a^1)) && echo $a
1
$ a=$(($a^1)) && echo $a
0
$ a=$(($a^1)) && echo $a
1
$ a=$((~$a)) && echo $a
-2
$ a=$((~$a)) && echo $a
1
$ a=$((~$a)) && echo $a
-2
$ a=$((~$a)) && echo $a
1

.. by the way, a bit of fun ...
Code:
$ cat tst
b_d(){ echo "obase=10;ibase=2;$1"|bc ; }
d_b(){ echo "obase=2;ibase=10;$1"|bc ; }

show_bit(){
a=$1
i=0
while [ $i -lt 32 ]
do
    let i+=1
     b=${a%?}
    c=${a#$b}

    if ( return $(($c^1)) ) ; then
        echo "bit$i=$c"
         eval bit$i=$c
    fi

    d=$(b_d "$a")
    t=$(("$d">>1))
    a=$(d_b "$t")
done
}

echo "Enter binary 32 bit number"
read num
show_bit $num
$ ksh tst
Enter binary 32 bit number
1000000000000100000000001000001
bit1=1
bit7=1
bit18=1
bit31=1
$


Last edited by ctsgnb; 12-03-2010 at 06:49 AM..
This User Gave Thanks to ctsgnb For This Post:
# 17  
Old 12-03-2010
ctsgnb, there is no need to use the bc utility if you are using ksh93. Your fun example can be done completely within the ksh93 shell
Code:
#!/bin/ksh93

show_bit() {
   integer -i2 a=2#$1
   integer -i2 mask=2#00000000000000000000000000000001

   for (( i=0; i < 32; i++, mask=$(( mask << 1)) ))
   do
       (( $mask & $a )) &&  echo "bit$((i+1))=1"
   done
}

read num?"Enter binary 32 bit number: "
show_bit $num

Code:
$ ./tst
Enter binary 32 bit number: 01000000000000100000000001000001
bit1=1
bit7=1
bit18=1
bit31=1
$

# 18  
Old 12-03-2010
I currently don't have ksh93
I just did dump that hugly script for fun Smilie

By the way you're right to point out those uncommon bit-mask stuff Smilie

i'll try to read more about it when i can save a bit more time.
# 19  
Old 12-03-2010
Folks i used the the examples you guys have written and applied them in my script , i have an another scenario in the same script ,where i have to break the whole binary and send them to different variables from $b1 to $b32
so that i can use the bit values of 0 and 1 as flags.....


for example my binary is

10101010101010101010101010101010... a 32 bit binary

so

a variable called $b1 will be assigned a value of 1 as there is a 1 in the first place so that i can write my script as

if
$b1=1 then do { ...........}

else
if $b1=0 then do { ........}
if
$b2=1 then do {.........}

else
if $b2=0 then do {.......}

i am trying to write these for 32 variables

Your help is appreciated.

Thanks
# 20  
Old 12-03-2010
Rather than create 32 variables, it is easier to create an array of 32 integers as shown in this example
Code:
#!/bin/ksh93

read num?"Enter binary 32 bit number: "

integer -i2 a=2#$num
integer -i2 mask=2#00000000000000000000000000000001

typeset -ui barray=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

for (( i=0; i < 32; i++, mask=$(( mask << 1)) ))
do
   nameref var=barray[$i]
   (( $mask & $a )) &&  var=1
done

# test the array values
if (( ${barray[0]} ))
then
   echo "barray[0] is 1"
else
   echo "barray[0] is 0"
fi

if (( ${barray[1]} ))
then
   echo "barray[1] is 1"
else
   echo "barray[1] is 0"
fi

# 21  
Old 12-06-2010
i tried to put in the array but failed

Guys,
i tried to put in the array ,but the commands typeset could not be recognised by the shell , nor did the eval command or nameref..... is there any work around , i have tried everything on the net ... if i missed out please correct and advise me.....
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