Pack/unpack binary string in Perl -


i attempting understand fragment of perl code. think purpose make binary string input integer, in reversed bit order (low bit on left, high bit on right). not understand pack/unpack doing input values however; appears incorrect.

consider test code:

for (my $i = 0; $i < 16; $i++) {      (my $j = 0; $j < 16; $j++) {          $x = $i * 16 + $j;         $x = unpack("b8", pack("u", $x));         printf $x;         print " ";     }     print "\n"; } 

this produces:

00000000 10000000 01000000 11000000 00100000 10100000 01100000 11100000 00010000 10010000 01010000 11010000 00110000 10110000 01110000 11110000 00001000 10001000 01001000 11001000 00101000 10101000 01101000 11101000 00011000 10011000 01011000 11011000 00111000 10111000 01111000 11111000 00000100 10000100 01000100 11000100 00100100 10100100 01100100 11100100 00010100 10010100 01010100 11010100 00110100 10110100 01110100 11110100 00001100 10001100 01001100 11001100 00101100 10101100 01101100 11101100 00011100 10011100 01011100 11011100 00111100 10111100 01111100 11111100 00000010 10000010 01000010 11000010 00100010 10100010 01100010 11100010 00010010 10010010 01010010 11010010 00110010 10110010 01110010 11110010 00001010 10001010 01001010 11001010 00101010 10101010 01101010 11101010 00011010 10011010 01011010 11011010 00111010 10111010 01111010 11111010 00000110 10000110 01000110 11000110 00100110 10100110 01100110 11100110 00010110 10010110 01010110 11010110 00110110 10110110 01110110 11110110 00001110 10001110 01001110 11001110 00101110 10101110 01101110 11101110 00011110 10011110 01011110 11011110 00111110 10111110 01111110 11111110 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 01000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000011 

so, going on here? seems 'high ascii' values (over 128) incorrectly converted, despite reading documentation pack , unpack cannot see going on here.

pack's u mode packs utf-8 character may or may not 1 byte. (the fact output begins 110 means the result 2 bytes long, that's different story.)

from documentation:

u - unicode character number. encodes character in character mode , utf-8 (or utf-ebcdic in ebcdic platforms) in byte mode. 

you should use c option ensure 1 byte result:

c - unsigned char (octet) value. 

that gives us:

for ( $i = 0; $i < 16; $i++ ) {      ( $j = 0; $j < 16; $j++ ) {          $x = $i * 16 + $j;         $x = unpack("b8", pack("c", $x));         printf $x;         print " ";     }     print "\n"; } 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -