Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Printing byte as 2 Hex characters

by Eliya (Vicar)
on May 30, 2012 at 15:12 UTC ( #973318=note: print w/ replies, xml ) Need Help??


in reply to Printing byte as 2 Hex characters

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.


Comment on Re: Printing byte as 2 Hex characters
Select or Download Code
Replies are listed 'Best First'.
Re^2: Printing byte as 2 Hex characters
by ShiningPerl (Novice) on May 30, 2012 at 16:02 UTC
    Thank you. This worked as intended.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://973318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-07-31 01:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls