in reply to [SOLVED] How to pack and unpack 16 bit decimal with leading zero

thanos1983:

No.

Leading zeroes are just formatting. It has nothing to do with the numeric value. So if you want leading zeroes in your output, you'll need to do it. One way would be to use something like sprintf:

$ perl -e 'printf "%05d",2911' 02911

Remember: a 16 bit number has 16 bits. Each bit is for a power of two in the result. There are no extra bits for specifying formatting. The fact that your code prints a number is because perl is converting the number to a string for you, using the default method--which is what we 'normally' want to see. If you want to specify a different format, use a formatting function (sprintf) to do it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: How to pack and unpack 16 bit decimal with leading zero
by thanos1983 (Parson) on Jul 27, 2015 at 10:36 UTC

    Hello roboticus,

    I guess the only solution is to use sprintf and in cases the number will 5 digits by default then there will be no zero. It should work, or I hope at least. :D

    Thank you for your time and effort.

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