Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

convert to binary number

by Anonymous Monk
on Jun 05, 2017 at 12:40 UTC ( [id://1192158]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I'm just a beginner, and i want to know what is the fastest/best way to convert a integer into a binary number?

Replies are listed 'Best First'.
Re: convert to binary number
by choroba (Cardinal) on Jun 05, 2017 at 12:52 UTC
    You can use the %b template in printf:
    my $int = 123; printf "%b\n", $int;

    Update: Another option is to use pack + unpack.

    my $int = 123; say unpack "B*", pack "n", $int;

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      If you need that binary sequence stored in a string value, you could use sprintflike so:

      my $int = 123; my $bin = sprintf "%b", $int; my $fix = sprintf "%08b", $int; print "Integer $int is binary $bin (eight-digit binary: $fix)\n";

Re: convert to binary number
by hippo (Bishop) on Jun 05, 2017 at 12:52 UTC
Re: convert to binary number
by BillKSmith (Monsignor) on Jun 05, 2017 at 22:34 UTC
    Is your number an integer? Is it stored in a file, in a perl variable, the perl code, or other? Do you want to address individual bits in the result, or do you want to display it on your screen? Write it to a file? Or other? You probably have already received the right answer. All you have to do is choose the one that guessed the right answers to these questions. That may not be easy. I suggest that you tell us what you want to accomplish. It probably can be done without your conversion at all. If it is required, we will be able to give you the answer that best fits your real needs.
    Bill

      I'm just a hobbyist, what i am trying to accomplish at this time is writing a script that converts normal numbers into binary ones. With your help it's working.

Re: convert to binary number
by anonymized user 468275 (Curate) on Jun 05, 2017 at 14:37 UTC
    Well, it is normally a binary number already, but not if it's embedded in something that Perl will treat as a string. For example an IP address expressed with 3 dot delimiters between four numbers and you want to convert the whole thing to one binary number. But yes, lookup pack in perldoc as choroba suggests.

    One world, one people

Re: convert to binary number
by Lotus1 (Vicar) on Jun 05, 2017 at 13:33 UTC

    It is already a binary number. Decimal, octal and hexadecimal are ways to display binary numbers that are easier for humans to comprehend. As choroba mentioned you can use printf to format how the number gets displayed.

      No, it's not. It's a number, and the OP really does want to convert it to binary. Binary is a text representation of a number just like decimal, octal and hexadecimal.

        It's electrons caught in little bits of silicon, pretending to be bits, pretending to be numbers.

        I was surprised by your statement "No, it's not. It's a number, ...". You were responding to my assertion that the integer was already in binary and could be printed in different bases with printf. Yes, an integer is a number but that doesn't mean it is not stored as binary in memory. Are you referring to assigning a number to a scalar variable such as my $num = 42 ?

        According to perlnumber an integer is represented in Perl as a native integer according to the C compiler used to build Perl. Maybe I have assumed Perl is more like C than it really is. I was referring to how it is stored in memory and that you can use printf to display the value in other bases or even in binary.

        The OP was vague and didn't provide examples so I'm wondering how to know what the OP really wanted.

Re: convert to binary number
by Anonymous Monk on Jun 05, 2017 at 13:08 UTC

    Tx

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1192158]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-20 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found