in reply to Is the documentation for Perl 5.20 'pack' correct?
I'd concur that the docs are misleading, if not outright wrong:
#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'Endian', CLEAN_AFTER_BUILD =>0; SV *hexDump( UV in ) { int i; char *p = (char*)∈ SV *out = newSVpvn( NULL, 0 ); for( i=0; i<8; ++i ) { sv_catpvf( out, "%02x", p[i] ); } return out; } END_C print hexDump( 72623859790382856 ); __END__ C:\test>endian Use of uninitialized value in subroutine entry at C:\test\endian.pl li +ne 19. 0807060504030201
(Aside: If anyone groks why I get the uninitialized value in subroutine entry warning; please enlighten me?)
But you have to factor in the context and tools you use to display the results.
For example, the first two below clearly demonstrate big and little endian respectively; the rest show how easy it is to get misleading results:
C:\test>p1 [0] Perl> print unpack 'C*', pack 'Q>', 72623859790382856;; 1 2 3 4 5 6 7 8 [0] Perl> print unpack 'C*', pack 'Q<', 72623859790382856;; 8 7 6 5 4 3 2 1 [0] Perl> print unpack 'H*', pack 'Q<', 72623859790382856;; 0807060504030201 [0] Perl> print unpack 'h*', pack 'Q<', 72623859790382856;; 8070605040302010 [0] Perl> print unpack 'H*', pack 'Q>', 72623859790382856;; 0102030405060708 [0] Perl> print unpack 'h*', pack 'Q>', 72623859790382856;; 1020304050607080 [0] Perl> print unpack 'b*', pack 'Q>', 72623859790382856;; 10000000 01000000 11000000 00100000 10100000 01100000 11100000 0001000 +0 [0] Perl> print unpack 'B*', pack 'Q>', 72623859790382856;; 00000001 00000010 00000011 00000100 00000101 00000110 00000111 0000100 +0 [0] Perl> print unpack 'b*', pack 'Q<', 72623859790382856;; 00010000 11100000 01100000 10100000 00100000 11000000 01000000 1000000 +0 [0] Perl> print unpack 'B*', pack 'Q<', 72623859790382856;; 00001000 00000111 00000110 00000101 00000100 00000011 00000010 0000000 +1
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Is the documentation for Perl 5.20 'pack' correct?
by syphilis (Archbishop) on Jul 07, 2015 at 02:06 UTC | |
by BrowserUk (Patriarch) on Jul 07, 2015 at 07:20 UTC | |
Re^2: Is the documentation for Perl 5.20 'pack' correct?
by ikegami (Patriarch) on Jul 07, 2015 at 00:08 UTC | |
by flexvault (Monsignor) on Jul 07, 2015 at 15:46 UTC |