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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |