Solved

# binary to hex conversion algorithm ?

Posted on 2001-08-21
1,744 Views
Does anyone have a good binary to hex algorithm handy?
ie.

1100001010  => 30A
0011110000  => 0F0

Alex
0
Question by:Alex_Tong
• 4

LVL 84

Expert Comment

ID: 6412352
print unpack"H*",pack"B*",'0000001100001010';
print unpack"H*",pack"B*",'0000000011110000';
printf "%x",0b1100001010;
printf "%03x",0b0011110000;
0

LVL 8

Expert Comment

ID: 6412584
Perl code to turn characters in a string into hexadecimal ASCII codes and back again.
=====================================================================================
\$string="A1B2";
\$out = unpack("H*",\$string);
print "Forward \$out\n";
\$str=pack("H*",\$out);
print "Reverse \$str\n";

--------------------
\$number1 = 0x1234;
\$number2 = 0xabcd;
printf( "%x\n", \$number1+\$number2)

0

Author Comment

ID: 6414370
When I try

\$temp = "00000000111";
print ("TEMP = \$temp\n");
print unpack"H*",pack"B*",\$temp;
exit();

The output becomes
TEMP = 00000000111
00e0

and I wanted 007

Alex
0

LVL 84

Expert Comment

ID: 6415620
\$temp = '0000000000000111';
print unpack"H*",pack"B*",\$temp;
0

LVL 84

Accepted Solution

ozo earned 75 total points
ID: 6415662
\$temp = "00000000111";
print scalar reverse unpack"h*",pack"b*",scalar reverse
\$temp;
0

LVL 84

Expert Comment

ID: 6415755
#in v5.6
printf "%03x",oct "0b\$temp";
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…