Hello Anonymous Monk,
I was afraid that the int size of "i" would be 32 bits, because I have been playing around and I could get the correct output with 32
perl -wMstrict -le 'print unpack("i",pack("B32","00000011"))' 3
I was hopping since I get no error when I am using pack I would not have any problems when I will use unpack.
perl -wMstrict -le 'print unpack("B8",pack("i",3))' 00000011
I noticed the: (Note the use of "B*" instead of "B$size".)
Ok then let me describe from the beginning what I am trying to achieve.
I want to convert decimal numbers on three-bit integer (signed and unsigned). So far I have managed to achieve it with the following piece of code:
#!/usr/bin/perl use strict; use warnings; sub dec2bin { my $bits = shift; my $size = shift; my $template = shift; return unpack("B$size", pack($template, $bits)); } sub bin2dec { my $bits = shift; my $size = shift; my $template = shift; return unpack($template, pack("B$size",substr("0" x $size . $bits +, -$size))); } my $int2bin = dec2bin( 3 , 8 , "c" ); print "Int2bin: ".$int2bin."\n"; my $binary2int = bin2dec( $int2bin , 8 , "c" ); print "Decimal from binary: ".$binary2int."\n";
My hesitation and the reason that I created the question was that I read on pack i A signed integer value. , I A unsigned integer value. (This 'integer' is _at_least_ 32 bits wide. Its exact size depends on what a local C compiler calls 'int'.) , c A signed char (8-bit) value. and finally C An unsigned char (octet) value.. This is the reason that I was trying to find a solution by using "i" and "I".
At this point I do not know if I should use "c" or "C" as an alternative to int and unsigned int for my task.
Thank you for your time and effort, reading and replying to my question.
In reply to Re^2: pack and unpack with 8 bit integers
by thanos1983
in thread pack and unpack with 8 bit integers
by thanos1983
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |