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
# 22  
Old 12-06-2010
Quote:
i tried to put in the array ,but the commands typeset could not be recognised by the shell
What shell are you actually using? In your initial post you told use you were using the Korn shell. That is why we provided answers that were specific to a modern Korn shell.
# 23  
Old 12-06-2010
$ksh
$uname -r

5

$uname -mrsn

Windows_NT 5 586
# 24  
Old 12-06-2010
This code worked fine for me in ksh (and ksh93). It does use external commands, so will not be quite as efficient as the pure ksh93 solution.
Note I used rev here to reverse the string most binary representations have the most significiant bit on the left (bit 0 is the right most bit):

Code:
#!/bin/ksh
read num?"Enter binary 32 bit number: "
set -- $(echo $num | rev | sed 's/./& /g' )
i=0
while [ $i -lt 32 ]
do
   let barray[$i]=${1:-0}
   [ $# -gt 0 ] && shift
   let i=i+1
done
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

fpmurphy, Even though I have ksh93 (on an AIX 5.3 box) the version must be too old to run your last solution:

Code:
$ echo ${.sh.version}
Version M-12/28/93e
$ ./tst
Enter binary 32 bit number: 10101010101010101010101010101010
./tst[12]: typeset: barray: reference variable cannot be an array.

# 25  
Old 12-06-2010
Thanks to CHUBLER...MURPHY AND ALL THE PRO'S

Thanks to CHUBLER...FPMURPHY AND ALL THE OTHER ANALYSTS....... IT WORKED.........
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