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

binary to hex conversion algorithm ?

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

1100001010  => 30A
0011110000  => 0F0

Alex
0
Alex_Tong
Asked:
Alex_Tong
  • 4
1 Solution
 
ozoCommented:
print unpack"H*",pack"B*",'0000001100001010';
print unpack"H*",pack"B*",'0000000011110000';
printf "%x",0b1100001010;
printf "%03x",0b0011110000;
0
 
shlomoyCommented:
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";    


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



0
 
Alex_TongAuthor 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

Please help,
Alex
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ozoCommented:
$temp = '0000000000000111';
print unpack"H*",pack"B*",$temp;
0
 
ozoCommented:
$temp = "00000000111";
print scalar reverse unpack"h*",pack"b*",scalar reverse
$temp;
0
 
ozoCommented:
#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.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now