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
# 8  
Old 12-02-2010
**wrong solution changed it in post #10---

Assuming the value is in $1
Code:
typeset -i2 b=0x$1
printf "%032d\n" ${b#2#} | sed 's/..../& /g'

to get it into variables
Code:
printf "%032d" ${b#2#} | sed 's/..../& /g' | read v1 v2 v3 v4 v5 v6 v7 v8

Or use an array in ksh93:
Code:
printf "%032d ${b#2#} | sed 's/..../& /g' | read -A V

**wrong solution changed it in post #10---

Last edited by Scrutinizer; 12-02-2010 at 11:38 AM.. Reason: **wrong solution changed it in post #10---
# 9  
Old 12-02-2010
Quote:
Code:
typeset -i2 b=0x$1
printf "%032d\n" ${b#2#}

Scrutinizer, what version of the Korn shell are you using? This overflows for me on both 32-bit and 64-bit platforms with latest ksh93 i.e.
Code:
 ./t abcdbcd
/t[5]: printf: warning: 1010101111001101101111001101: overflow exception
0000000000009223372036854775808

This User Gave Thanks to fpmurphy For This Post:
# 10  
Old 12-02-2010
Hi fp, I am using version sh (AT&T Research) 93t+ 2009-05-01. I am getting it too now. Will look into it...

---------- Post updated at 15:50 ---------- Previous update was at 15:34 ----------

Changed it to string padding:
Assuming the value is in $1
Code:
typeset -i2 b=0x$1

Code:
printf "%32s\n" ${b#2#} | sed 's/ /0/g;s/..../& /g'

to get it into variables
Code:
printf "%32s" ${b#2#} | sed 's/ /0/g;s/..../& /g' | read v1 v2 v3 v4 v5 v6 v7 v8

Or use an array in ksh93:
Code:
printf "%32s" ${b#2#} | sed 's/ /0/g;s/..../& /g' | read -A V

--
In ksh93 one can do this :
Code:
typeset -ui2 b=0x$1

if it is an unsigned number
# 11  
Old 12-02-2010
result of hex to bin

Allmight Forum ,
here are my results , i have taken the excerpts given by scrutinizer,fpmurphy and other analysts ...
this code works but gets result in this way

2#1000000000000100000000001000001

on execution

and so i trimmed it to extract just binary and then the flags

integer -i2 mask=16#F0000000

integer -i2 in=16#$HEX ..variable where i am getting my hexadecimal into

the $in result is 2#1000000000000100000000001000001

var4=${in#*#}.... trimmed it to get 1000000000000100000000001000001


i need your help in interpreting this above binary to get just the 1 's and their places ...

for example there is a 1 in the 1 st place so b1= 1 ... and since there is 1 in 14th place so b14 = 1 and 25th place so b25 =1 and so on and so forth.....

can we put this in a loop and spit the results in to variables from b1 to b32 ...or b0 to b31....

Thanks One and All
# 12  
Old 12-02-2010
Code:
$ echo "1000000000000100000000001000001" | sed 's/ */ /g;s/^ //;s/ $//' | tr ' ' '\n' | cat -n |sed '/0$/d;/1$/s/^[[:blank:]]*/b/;s/[[:blank:]]1/=1/'
b1=1
b14=1
b25=1
b31=1
$

This User Gave Thanks to ctsgnb For This Post:
# 13  
Old 12-02-2010
Why not put the bits in an array? (you will probably find them easier to work with in your script later):

Code:
$ eval B="( $(echo 1000000000000100000000001000001 | sed 's/./& /g') )"
 
$ for ((i=0; i<31; i++))
do
    [ ${B[$i]} -eq 1 ] && echo $i
done
0
13
24
30


Last edited by Chubler_XL; 12-02-2010 at 10:15 PM..
This User Gave Thanks to Chubler_XL For This Post:
# 14  
Old 12-02-2010
Quote:
Originally Posted by ctsgnb
Code:
$ echo "1000000000000100000000001000001" | sed 's/ */ /g;s/^ //;s/ $//' | tr ' ' '\n' | cat -n |sed '/0$/d;/1$/s/^[[:blank:]]*/b/;s/[[:blank:]]1/=1/'
b1=1
b14=1
b25=1
b31=1
$


can we assign these values into variables........such as $b1, $b14,$b25,$b31, for use in the rest of the script.....

Thanks
Venu
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