I wonder why you want to do that. This almost always unneeded in Perl, because Perl usually does the conversion for you. Internally, numbers are used as binary numbers and Perl usually computes with double-precision floating-point values. When you enter a number in decimal format, Perl usually stores it in binary format (well, in some cases, it might initially store it as a string if Perl does not know that it is supposed to be a number, but it will do the conversion when you use that string as a number, for example in an arithmetic operation). When you are trying to "convert" numbers either way, you actually don't convert them, you are only converting string representations of these numbers. When you write:
my $value1 = 255; my $value2 = 0377; # 377 octal, equal to 255 decimal my $value3 = 0xff; # FF hex, same as 255 decimal my $value4 = 0b11111111; # also 255 decimal
all these values are the same to Perl.

The only thing for which I can see the conversions you are trying to do to be useful is for input and output of special representations of those numbers. Or, of course, for training purposes but you will not have to use that very often.

I can remember of one single case where I had to do that in Perl, for modeling a game with white and black stones where is was convenient to represent the game board (and the moves) with 16-bit integers, where, for example, a 0 would represent a white stone and 1 a black stone (still, although my program was using heavily the bit-wise operators, I needed binary representation only for the purpose of displaying conveniently debugging information, the program itself did not need it).

The octal representation is sometimes needed for such thing as Unix file permissions. Sometimes you might use bit vectors and need to see what it looks like, or need to visualize individual bits for other low level operations, but it is still rather uncommon in day to day Perl programming to need to use the binary representation of numbers.


In reply to Re: How to convert negative and positive floating points to binary and vice versa by Laurent_R
in thread How to convert negative and positive floating points to binary and vice versa 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.