Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Printing byte as 2 Hex characters

by ShiningPerl (Novice)
on May 30, 2012 at 14:40 UTC ( [id://973301]=perlquestion: print w/replies, xml ) Need Help??

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

I am studying video and other kind of files that deals with packed data. I want to break down a flv file into its byte and printing on the screen but I face alot of difficulties. My desired output to print 0b11110000 as 0xF0 but all I got was 0. Is there something wrong with the print format?

#! /usr/bin/perl $data_file="1.flv"; open (FILE, $data_file) or die ("Could not open file!"); my $len=0; while($len!=7){ read (FILE, $buffer, 1); print "Char Format: "; print "$buffer "; print "Hex Format: "; printf ("%lX ", $buffer); print " Test Print Format"; $test=255; print " ".$test." "; printf ("%lX", $test); print "\n"; $len++; } print "\n=====End of Script====="; close(FILE);

Replies are listed 'Best First'.
Re: Printing byte as 2 Hex characters
by Eliya (Vicar) on May 30, 2012 at 15:12 UTC
    printf ("%lX ", $buffer);

    You probably want ord:

    printf ("0x%lX ", ord $buffer);

    because what you have in $buffer is a single byte string, not a number.

    A more direct way might be to use unpack "H*", ...

    $ perl -E'say unpack "H*", "foobar"' 666f6f626172

    or maybe unpack "(H2)*", ..., as in

    $ perl -E'say "0x$_" for unpack "(H2)*", "foobar"' 0x66 0x6f 0x6f 0x62 0x61 0x72

    in which case your buffer could consist of more than one byte.

      Thank you. This worked as intended.
Re: Printing byte as 2 Hex characters
by ikegami (Patriarch) on May 30, 2012 at 15:38 UTC
    [ 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

      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
      ord was very useful. Thanks for your reply
Re: Printing byte as 2 Hex characters
by SuicideJunkie (Vicar) on May 30, 2012 at 14:59 UTC

    You should include the output of your program in a second code tag. Perhaps the value you read was zero? Pipe it to a file and copy paste for accuracy.

    Also, I expect you probably would prefer the format printf('0x%2X'...

    PS: an error message of 'Could not open file' is very vague. Try "Could not open file '$data_file': OS told me '$!'\n"

Re: Printing byte as 2 Hex characters
by toolic (Bishop) on May 30, 2012 at 14:51 UTC
      I am hoping to extend the knowledge gained in manipulating FLV files to other formats.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found