in reply to How to convert between decimal and binary for positive/negative numbers?

Template 'N' is for unsigned 32-bit integers (ie. no negative values).

You'll need to use 'l' or 'i' or 'j' for signed 32-bit integers. See the pack documentation to select the appropriate one for your purpose.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: How to convert between decimal and binary for negative numbers?

Replies are listed 'Best First'.
Re^2: How to convert between decimal and binary for negative numbers?
by thanos1983 (Parson) on Sep 18, 2014 at 14:48 UTC

    Hello BrowserUk

    Thank you for your time and effort to help me with my problem. Thank you for the tip.

    Based on my code if I am using an int size 32 bit works fine. But I was trying to get a solution for small negative numbers, no more than -127 which is the limit of 8 bit int. A tried a dew different solutions but none successfully so far, any ideas?

    #!/usr/bin/perl use strict; use warnings; my @array = (3,-3); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_,32,"i"); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b,"i"); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_,8,"N"); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b,"N"); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } sub dec2bin { my $bits = shift; my $possition = shift; my $template = shift; my $str = unpack("B32", pack($template, $bits)); $str = substr $str, -$possition; return $str; } sub bin2dec { my $bits = shift; my $template = shift; return unpack($template, pack("B32", substr("0" x 32 . $bits , -32 +))); } __END__ Value: 3 is possitive Possitive binary: 00000011 Possitive_binary back to decimal: 3 Value: -3 is negative Negative binary: 11111101111111111111111111111111 Negative_binary back to decimal: -3
    Seeking for Perl wisdom...on the process of learning...not there...yet!
      But I was trying to get a solution for small negative numbers, no more than -127 which is the limit of 8 bit int.

      If you want to pack 8-bit numbers, do not use 32-bit templates; nor 'B32'.

      Think! read, and think again.

      (Hint:8-bit signed numbers; 'B8' and 'c'...)


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Hello again BrowserUk,

        I could not get a better answer than that. :D I will work on it, I am under some pressure to finish something else but I would love to work on it. I need to read thoroughly the documentation I agree with you 100%. Again thank you for your time and effort. I will get back to you with an answer in a few days. Hopefully soon. :D

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