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.

Seeking for Perl wisdom...on the process of learning...not there...yet!

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.