Hexadecimal to Binary conversion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hexadecimal to Binary conversion
# 1  
Old 08-20-2013
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 got
Code:
echo "ibase=16;obase=2;AF6D"|bc

workd for me..thanks alot all
cheers

Last edited by Scott; 08-20-2013 at 05:01 AM.. Reason: Code tags
# 2  
Old 08-20-2013
Code:
echo "AF6D" | perl -ne 'printf "%016b\n", hex($_)'

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 08-20-2013
bc

Code:
echo "ibase=16 ; obase=2 ; AF6D" | bc

These 2 Users Gave Thanks to krishmaths For This Post:
# 4  
Old 08-20-2013
awk
Code:
echo "AF6D" | 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 ""
	}'
1010111101101101

Edit: other version found on unix.com
Code:
echo "AF6D" | awk --non-decimal-data ' {
for(i=lshift(1,15);i;i=rshift(i,1))
printf "%d%s", (and(("0x"$1)+0,i) > 0), ++c%4==0?" ":"";
printf"\n"; }'
1010 1111 0110 1101


Last edited by Jotne; 08-20-2013 at 05:13 AM..
This User Gave Thanks to Jotne For This Post:
# 5  
Old 08-20-2013
Remember!

Be aware......
Code:
echo "ibase=16 ; obase=2 ; AF6D" | bc

......strips leading zeros...

AF6D gives 16 bits; highest bit is a "1"...
7F6D gives 15 bits; leading "0" is stripped; highest bit is still "1"...
# 6  
Old 08-20-2013
Maybe a something like this would help:-
Code:
typeset -Z16 binval=`echo "ibase=16 ; obase=2 ; 7F6D" | bc`
echo $binval

Just a thought.



Robin
# 7  
Old 08-20-2013
Done in bash, should work in recent other shells:
Code:
VALUE=$((0xAF6D))
BITS=32
for ((i=BITS-1;i>=0;i--)); do printf "%d" $(( (VALUE>>i)%2 )); done; printf "\n"
00000000000000001010111101101101

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

Conversion from Hexadecimal to binary

How can I convert hexadecimal values to Binary from the second field to the end Input: WS-2 23 345 235 DT-3 45 4A3 000 pp-2 76 300 E4 Output: WS-2 100011 1101000101 1000110101 DT-3 1000101 10010100011 000 pp-2 1110110 1100000000 11100100 (16 Replies)
Discussion started by: aydj
16 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