Conversion from Hexadecimal to binary


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conversion from Hexadecimal to binary
# 1  
Old 03-11-2014
Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end

Input:

Code:
WS-2 23 345 235
DT-3 45 4A3 000
pp-2 76 300 E4

Output:

Code:
WS-2 100011 1101000101 1000110101
DT-3 1000101 10010100011 000
pp-2 1110110 1100000000 11100100

# 2  
Old 03-11-2014
Check "More UNIX and Linux Forum Topics You Might Find Helpful" suggested at the bottom of this thread
This User Gave Thanks to anbu23 For This Post:
# 3  
Old 03-11-2014
I have gone through but could not get help
# 4  
Old 03-11-2014
This works in the bash shell. It is a one-off hack, IMO. perl or some other interpreted language would be more efficient.
Code:
#!/bin/bash
#obase.shl - convert to binary from hex
while read rec
do
a=( $rec)
 (echo ${a[0]}
  echo "ibase=16; obase=2; ${a[1]}"  | bc -l
  echo "ibase=16; obase=2; ${a[2]}"  | bc -l) | tr -s  '\n' ' '
  echo "ibase=16; obase=2; ${a[3]}"  | bc -l 
done < infile

Code:
$ ./obase.shl
WS-2 100011 1101000101 1000110101
DT-3 1000101 10010100011 0
pp-2 1110110 1100000000 11100100

$

No time left --- I cannot fiddle with the remaining problem : 000 -> output as 000. fix that one yourself.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 03-11-2014
See this might help you

Code:
awk  '
 
function bits2str(bits,        data, mask)
     {
         if (bits == 0)
             return "0"
     
         mask = 1
         for (; bits != 0; bits = rshift(bits, 1))
             data = (and(bits, mask) ? "1" : "0") data
     
         while ((length(data) % 8) != 0)
             data = "0" data
     
         return data
     }
     
     {for(i=2;i<=NF;i++)$i = bits2str(strtonum("0x"$i))}1' file

https://www.gnu.org/software/gawk/ma...Functions.html

String Functions - The GNU Awk User's Guide
This User Gave Thanks to Akshay Hegde For This Post:
# 6  
Old 03-11-2014
Quote:
Originally Posted by jim mcnamara
This works in the bash shell. It is a one-off hack, IMO. perl or some other interpreted language would be more efficient.
Code:
#!/bin/bash
#obase.shl - convert to binary from hex
while read rec
do
a=( $rec)
 (echo ${a[0]}
  echo "ibase=16; obase=2; ${a[1]}"  | bc -l
  echo "ibase=16; obase=2; ${a[2]}"  | bc -l) | tr -s  '\n' ' '
  echo "ibase=16; obase=2; ${a[3]}"  | bc -l 
done < infile

Code:
$ ./obase.shl
WS-2 100011 1101000101 1000110101
DT-3 1000101 10010100011 0
pp-2 1110110 1100000000 11100100

$

No time left --- I cannot fiddle with the remaining problem : 000 -> output as 000. fix that one yourself.
0 instead of 000 output is ok
# 7  
Old 03-11-2014
The simplest one-liner I could craft:
Code:
( echo ibase=16\; obase=2; awk '$1 = "\""$1"\n\""' OFS=\; file ) | bc | paste - - - -

Regards
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Hexadecimal to binary operation

Dear all, I am trying to write c-program to read the following file containing hexadecimal values (snippet of big data file). I want to combine two hexadecimal values together like A0A03E01 and then would like to have the binary equivalent to perform further test on it. Unfortunately, it failed... (16 Replies)
Discussion started by: emily
16 Replies

2. Shell Programming and Scripting

Hexadecimal to Binary conversion

Hi Guys, Is it possible to convert the hexadecimal to Binary by unix command.....I could not figure out.... If I need to convert AF6D to binary...what could be the way to do? Thanks in advance!! ---------- Post updated at 02:57 AM ---------- Previous update was at 02:42 AM ---------- I... (6 Replies)
Discussion started by: Indra2011
6 Replies

3. Shell Programming and Scripting

NAWK conversion of hexadecimal input to decimal output via printf, I am close I can feel it

I have searched and the answers I have found thus far have led me to this point, so I feel I am just about there. I am trying to convert a column of hexadecimal to decimal values so that I can filter out via grep just the data I want. I was able to pull my original 3 character hex value and... (10 Replies)
Discussion started by: PCGameGuy
10 Replies

4. Solaris

binary conversion

Why would a binary which was compiled on a Solaris-10 not be runnable in a SunOS 5.10? (they are supposed to be precisely equivalent). When I run the file command on it, it says: ELF 32-bit LSB executable 80386 Version 1, dynamically linked, not stripped, no debugging information available... (10 Replies)
Discussion started by: steve701
10 Replies

5. Shell Programming and Scripting

binary to ascii conversion

Hi, I have got a library file, created by compiling C code. The file information with "file" command, gives it a "application/x-archive" type file. I want to extract the release string of my software from this file, so that i can know which version of C files were used to create the lib. Can... (3 Replies)
Discussion started by: atulmt
3 Replies

6. Shell Programming and Scripting

binary file conversion

Hello folks, i have a binary text file but i am not able to convert into text format, please suggest. thanks. (2 Replies)
Discussion started by: learnbash
2 Replies

7. Programming

Binary conversion function

Is/are there any function(s) in C that convert(s) character/ASCII/Decimal to binary and vice versa? what about bcopy and strcpy? (1 Reply)
Discussion started by: Peevish
1 Replies

8. Shell Programming and Scripting

Decimal to Hexadecimal conversion

Hi frnds :) I need a small help... I have a very long file containing 20 digits decimal number which i want to convert into the corresponding 16 digit hexadecimal values. File looks like.... 11908486672755551741 05446378739602232559 04862605079740156652 . . . I tried the script for i... (7 Replies)
Discussion started by: vanand420
7 Replies

9. UNIX for Dummies Questions & Answers

Binary file conversion

All, I want to convert multiple \0 005 characters to line feed 012 character in a binary file to make to readable. Here is the sample od -c file output: 0000000 254 355 \0 005 s r \0 * c o m . c i s c Here is the sample od -b file output: 0000000 254 355 000... (0 Replies)
Discussion started by: bubba112557
0 Replies

10. Shell Programming and Scripting

shell script conversion to binary

My question is: i have a script called getevent to run i just call ./getevent can i convert this to make it binary executable and not letting my clients open it and see the code.??:( I am using Solaris 8. (3 Replies)
Discussion started by: bcheaib
3 Replies
Login or Register to Ask a Question