I want to convert decimal numbers on three-bit integer (signed and unsigned).

Ok, but why? That's what is meant by an XY Problem - are you doing this for example because you're communicating with some other software that requires data in this format (if so, what is that format? perhaps show some sample data?), just for practice / fun, etc.? It can help to know what the broader picture is. In this case the main reason being that Perl's pack and unpack are not always the right tool for the job (also there are some pack templates that I'd stay away from as they can get too complex). So far, I've only found them useful for decoding C structs, handling binary data in network protocols, or for debugging.

Anyway, to get to your questions:

... This is the reason that I was trying to find a solution by using "i" and "I".

I find the best way to think about pack templates is C data types. Think of i as int, and that'll make it clear that i is (unfortunately) not "as many bits as I want".

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.

As said before, unless you know that you want the system-dependent formats, stick with the system-independent formats cCnNvV, perhaps with the ! modifier to make them signed, or sl with one of the <> modifiers to force endianness. Then pick according to required size, signedness, and endianness.

I want to convert decimal numbers on three-bit integer (signed and unsigned).

A three-bit signed integer? I don't think Perl has built-in support for that, so you'll probably need to build your own Sign extension, as some quick searching has not (yet) shown me any CPAN modules. If you want to do it on binary values, see perlop for the bitwise ops, also maybe vec; or you could do it on the string representation (which will probably be a little slower). Or, with just 8 possible values, a look-up table may be the easiest way!


In reply to Re^3: pack and unpack with 8 bit integers by Anonymous Monk
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.