Hello Monks,

More I get closer to my solution more problems I discover. I am trying to convert decimals into 16 bits.

I am using pack and unpack, which works perfect when the first digit of the decimal number is not zero:

I know it does not make sense to have a decimal starting with zero. The reason is that I have a number such as 0.02911 where I am splitting the string before the dot and after so I can send them separately.

So my alternative option would be to convert the decimal to binary B16 send in and then reverse the process. Cookbook 2.4. Converting Between Binary and Decimal

Sample of code with both proposed solutions:

#!/usr/bin/perl use warnings; use strict; my $sampleDecimal = "02911"; my $uint16 = pack 'n', $sampleDecimal; my $decimal = unpack 'n', $uint16; print "Decimal: " . $decimal . "\n"; my $binaryStr = desimalToBinary( $sampleDecimal , 16 , 'n' ); print "Binary: " . $binaryStr . "\n"; my $decimalStr = binaryToDecimal( $binaryStr , 16 , 'n' ); print "Decimal from binary: " . $decimalStr . "\n"; sub binaryToDecimal { my $binary = shift; my $bitSize = shift; my $template = shift; return unpack($template, pack("B$bitSize", substr("0" x $bitSize . + $binary, -$bitSize))); } sub desimalToBinary { my $decimal = shift; my $bitSize = shift; my $template = shift; my $bitStr = unpack("B$bitSize", pack($template, $decimal)); $bitStr =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros return $bitStr; } __END__ Decimal: 2911 Binary: 101101011111 Decimal from binary: 2911

Unfortunately, both proposed solutions do give the same result. A non leading zero decimal. I know I could add manually the leading zero, but in cases the number produced with a non zero digit, then I will have completely wrong number.

So the question, is it possible to get the leading zero through pack and unpack using 'n' as a template?

Update 2: Well it seems that the best solution is to use, sprintf as roboticus proposed.

Sample of code/solution:

#!/usr/bin/perl use warnings; use strict; my $sampleDecimal = "02911"; my $uint16 = pack 'n', $sampleDecimal; my $decimal = unpack 'n', $uint16; $decimal = sprintf("%05d", $decimal); print "Decimal: " . $decimal . "\n"; my $binaryStr = desimalToBinary( $sampleDecimal , 16 , 'n' ); print "Binary: " . $binaryStr . "\n"; my $decimalStr = binaryToDecimal( $binaryStr , 16 , 'n' ); $decimalStr = sprintf("%05d", $decimalStr); print "Decimal from binary: " . $decimalStr . "\n"; sub binaryToDecimal { my $binary = shift; my $bitSize = shift; my $template = shift; return unpack($template, pack("B$bitSize", substr("0" x $bitSize . + $binary, -$bitSize))); } sub desimalToBinary { my $decimal = shift; my $bitSize = shift; my $template = shift; my $bitStr = unpack("B$bitSize", pack($template, $decimal)); $bitStr =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros return $bitStr; } __END__ Decimal: 02911 Binary: 101101011111 Decimal from binary: 02911

Thank you for your time and effort.

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

In reply to [SOLVED] How to pack and unpack 16 bit decimal with leading zero 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.