in reply to Printing byte as 2 Hex characters
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 0b11110000 | my $num = oct($input); |
If your input is the number 240 | my $num = $input; |
If your input is the character 240 | my $num = ord($input); |
If your input is the character 240 | my $num = unpack('C', $input); |
do something with it, then format the number as desired.
If your desired output is the string 0xF0 | my $output = sprintf("0x%02X", $num); |
If your desired output is the number 240 | my $output = $num; |
If your desired output is the character 240 | my $output = chr($num); |
If your desired output is the character 240 | my $output = pack('C', $num); |
Looking at your code, you want the the string 0xF0 from character 0b11110000, so
orprintf("0x%02X", ord($input));
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 | |
by KevinZwack (Chaplain) on May 31, 2012 at 01:21 UTC | |
Re^2: Printing byte as 2 Hex characters
by Anonymous Monk on Aug 29, 2013 at 11:31 UTC |