The stupid question is the question not asked PerlMonks

### I'm looking for the faster way to convert char 2 Bin and Bin 2 char (8bits)

by DarkGoth (Acolyte)
 on Jan 25, 2001 at 22:35 UTC Need Help??
DarkGoth has asked for the wisdom of the Perl Monks concerning the following question:

I've seen many means to return the bin value of an integer and a question came to me ...
How can i convert a char 2 bin and bin 2 char?
typing char2bin(a) --> 001000001 (not 1000001)

I have found some solutions to this one but i guess there's a better one ...
i guess in one line.
I have made some test for the contrary : bin 2 char, but a problem came to me : it returns me the ascii value.
I have made a pack "C" then ...
but i'd like a quicker and easier solution.

so, if you'v got an idee ... i would be thanxfull :)~
• Comment on I'm looking for the faster way to convert char 2 Bin and Bin 2 char (8bits)

Replies are listed 'Best First'.
Re: I'm looking for the faster way to convert char 2 Bin and Bin 2 char (8bits)
by eg (Friar) on Jan 26, 2001 at 00:32 UTC

If you want a string of 0's and 1's, use pack and unpack with the template "B*" (descending) or "b*" (ascending).

Is that what you're looking for?

Ok, that's right ;) thanx.
That was so easy in fact ...
Re: I'm looking for the faster way to convert char 2 Bin and Bin 2 char (8bits)
by Fixer (Acolyte) on Jan 26, 2001 at 03:28 UTC
To get the byte (char) representation of a bit string:
\$char = pack("b8","01010101");
To get the bit_string of a char/byte:
\$bit_string = unpack("b8",\$char);
Thanx, that was exactly what i looked for ;)~~~~
Re(golf): I'm looking for the faster way to convert char 2 Bin and Bin 2 char (8bits)
by jynx (Priest) on Jan 27, 2001 at 03:17 UTC
beware before!

Yesterday i found an amusing little routine to do decimal to binary (not the same drek everyone did in school as i understand it). So i converted it to perl and felt like posting it. Unfortunately i may have messed up something, so if someone (like maybe IO) could tell me if i did or not that would be sweet.

```sub dec2bin {
my \$num = shift;
my @num;  my \$i = 0;

(push @num, ((\$num & 2**\$i) == 2**\$i) ? 1 : 0) and \$i++ until (2**\$
+i > \$num);
return 0 + join '', reverse @num;
}

It is completely untested, but it should work (as if that's any boon to a programmer)

jynx

update: changed the \$n to \$num on line 5 according to IO's suggestion (thank you).

Testing it would have found
Name "main::n" used only once: possible typo at - line 5.
But besides that, ** is a more expensive operation than you need here especially doing it 3 times per iteration.
using 1<<\$iin place of 2**\$i would be a bit faster.
```  my \$i = 1;
(push @num, ((\$num & \$i) ? 1 : 0)) and \$i<<=1 until \$i > \$num;
is even better. And
(push @num, ((\$num & 1) ? 1 : 0)) and \$num>>=1 until 1 > \$num;
still faster...

Create A New User
Node Status?
node history
Node Type: perlquestion [id://54317]
Approved by root
help
Chatterbox?
 [Corion]: This evening, I'll kill that wrapper again, and just call LoadFile() in the plugin class directly instead of creating a go-between object for no real gain. [Corion]: Writing these import plugins was really nice though - in about 2 hours, I had imports for CSV, YAML and JSON, and adding XLS(X), SQLite (or DBI) data sources is also trivial. I'm idly wondering about separating the plugin into transport+parser, so ... [Corion]: ... http:// URLs could be retrieved and then parsed, but I think that that would be overkill for a toy static site generator ;)

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-05-22 11:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (163 votes). Check out past polls.

Notices?