Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Printing byte as 2 Hex characters

by ikegami (Pope)
on May 30, 2012 at 15:38 UTC ( #973322=note: print w/replies, xml ) Need Help??


in reply to Printing byte as 2 Hex characters

[ I went sideways a bit since this is a learning exercise. Hopefully, this will put you in the right mindset to solve similar problems in the future. ]

My desired output to print 0b11110000 as 0xF0 but all I got was 0

This alone isn't very clear.

Is your input the string 0b11110000, the number 0b11110000 (240) or the character 0b11110000 (240)?

Is your desired output the string 0xF0, the number 0xF0 (240) or the character 0xF0 (240)?

One would generally convert the input to a number,

If your input is the string 0b11110000my $num = oct($input);
If your input is the number 240my $num = $input;
If your input is the character 240my $num = ord($input);
If your input is the character 240my $num = unpack('C', $input);

do something with it, then format the number as desired.

If your desired output is the string 0xF0my $output = sprintf("0x%02X", $num);
If your desired output is the number 240my $output = $num;
If your desired output is the character 240my $output = chr($num);
If your desired output is the character 240my $output = pack('C', $num);

Looking at your code, you want the the string 0xF0 from character 0b11110000, so

printf("0x%02X", ord($input));
or
printf("0x%02X", unpack('C', $input));

oct, ord, unpack, sprintf, printf, chr, pack

Replies are listed 'Best First'.
Re^2: Printing byte as 2 Hex characters
by ShiningPerl (Novice) on May 30, 2012 at 16:01 UTC
    Thank you so much, this is so much detail contributed on your side.

      Here is one I didn't see listed that I often use for debugging...

      printf "0x%*v2.2X\n", ' ', $payload;

      If $payload is several bytes of data, this will print something like:

      0x01 02 03 CF
Re^2: Printing byte as 2 Hex characters
by Anonymous Monk on Aug 29, 2013 at 11:31 UTC
    ord was very useful. Thanks for your reply

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://973322]
help
Chatterbox?
Lady_Aleena remembers winning dodgeball and wonders why parents who played it will not let their kids play it.
[marioroy]: Disciplus My wife and I went on vacation. At work, I was stuck polling SNMP from 20 million devices. It would hang at 80,000. On the mist boat, hear a voice to enable grace in the design that 10x and more performance awaits.
[Lady_Aleena]: Women need challenges too karlgoethebier. 8)
[marioroy]: s/hear/heard/
[karlgoethebier]: Discipulus: No. See https://en. wikipedia.org/ wiki/Tribe_( Native_American)
[karlgoethebier]: They took Unix from some aliens
[marioroy]: Perl is so powerful that it can poll 40 metrics from 20 million devices in 40 minutes using 4 nodes only.
[karlgoethebier]: http://www. uncoveror.com/ aliens.htm
[Lady_Aleena]: marioroy, I can't decide whether or not to move my RolePlaying:: Random:: modules to just Random::. I'm not as sophisticated as most here.
[marioroy]: Naming things can be difficult, sometimes more so that the actual code :)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-05-29 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?