Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

binary to hex conversion algorithm ?

Posted on 2001-08-21
6
Medium Priority
?
1,754 Views
Last Modified: 2010-08-05
Does anyone have a good binary to hex algorithm handy?
ie.

1100001010  => 30A
0011110000  => 0F0

Alex
0
Comment
Question by:Alex_Tong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 84

Expert Comment

by:ozo
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

by:shlomoy
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";    


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



0
 

Author Comment

by:Alex_Tong
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

Please help,
Alex
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:ozo
ID: 6415620
$temp = '0000000000000111';
print unpack"H*",pack"B*",$temp;
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
ID: 6415662
$temp = "00000000111";
print scalar reverse unpack"h*",pack"b*",scalar reverse
$temp;
0
 
LVL 84

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question