Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Bin 2 Hex

by Streen (Sexton)
on Jun 02, 2004 at 13:45 UTC ( [id://359429]=perlquestion: print w/replies, xml ) Need Help??

Streen has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to convert a binary value into an hexadecimal one. I tried serveral ways, but I didn't succeed. I tried sprintf and the pack/unpack... but It didn't work. I have an binary value which is always 19 bits long, and I would like to convert it into an hexvalue. For example: 0000000011100000011 should be converted into 00703. Some suggestions how I can make this? Some of my code snippest, how I tried it:
my $hex = sprintf("%x", '0000000011100000011'); or my $hex = unpack "I", '0000000011100000011'; $hex = sprintf("%x", $hex);
I also tried several other versions, but none of them worked..
Thanks for help..

Replies are listed 'Best First'.
Re: Bin 2 Hex
by tinita (Parson) on Jun 02, 2004 at 13:55 UTC
    uhm, what about:
    my $hex = sprintf "%05x", oct '0b'.$binary;
    see perlfunc (hex, oct, sprintf)
      Oh, thanks a lot...
      Now I tried to reverse it (hex->bin), how can I make this?
      Okay this was easier than I thought, it did not function because I had a typo:
      hex2bin:
      my $bin = sprintf "%19b", oct '0x'.$hex;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://359429]
Approved by tinita
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 07:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found