• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1762

# binary to hex conversion algorithm ?

Does anyone have a good binary to hex algorithm handy?
ie.

1100001010  => 30A
0011110000  => 0F0

Alex
0
Alex_Tong
• 4
1 Solution

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

Commented:
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 Commented:
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

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

Commented:
\$temp = "00000000111";
print scalar reverse unpack"h*",pack"b*",scalar reverse
\$temp;
0

Commented:
#in v5.6
printf "%03x",oct "0b\$temp";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.