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,
| [reply] [d/l] [select] |
|
my $int = 123;
my $bin = sprintf "%b", $int;
my $fix = sprintf "%08b", $int;
print "Integer $int is binary $bin (eight-digit binary: $fix)\n";
| [reply] [d/l] [select] |
Re: convert to binary number
by hippo (Bishop) on Jun 05, 2017 at 12:52 UTC
|
| [reply] |
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.
| [reply] |
|
| [reply] |
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.
| [reply] |
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.
| [reply] [d/l] |
|
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.
| [reply] |
|
It's electrons caught in little bits of silicon, pretending to be bits, pretending to be numbers.
| [reply] |
|
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.
| [reply] [d/l] [select] |
|
|
|
Re: convert to binary number
by Anonymous Monk on Jun 05, 2017 at 13:08 UTC
|
| [reply] |