Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: sprintf %X endianness

by andal (Hermit)
on May 15, 2013 at 07:11 UTC ( #1033622=note: print w/replies, xml ) Need Help??

in reply to sprintf %X endianness

Very strange question. Function "sprintf" (in C library) interprets the numbers according to the endiannes of the machine. If you put the number in wrong endiannes, then the output of sprintf shall be wrong. I mean, the output of sprintf is text and this text is supposed to be read from left to right.

How can this be "portable" or "not portable"?

Normally one needs to worry only about converting sequences of bytes received from external source to the numbers as program understands them. For that the "unpack" function is used. If you know that your external source feeds you with numbers presented as bytes in big-endian order, then you use "N", or "n", or "L>" etc. If you know that it is little-endian, then you use "V", or "v", or "L<" etc. If you don't know what order was used, then you are in trouble :) Once you handle this part correctly, you don't need to worry about sprintf or any other system function anymore.

Internal numbers will always be handled "correctly" by sprintf. The output of sprintf shall be matching to the big-endian representation of the provided number.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033622]
[AnomalousMonk]: LanX: I should have written "vessel". Rama was (is? - I'm still hoping) no asteroid.
[erix]: itym vehicle (vessel is for water-vehicles)
[erix]: then again you may be a native speaker and I'd do better to shut up :)

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2017-11-22 17:27 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (327 votes). Check out past polls.