in reply to int2bin
Nice code. Perl has some slick ways of doing this sort of thing. You may have set off a round of golf. My ungolfed way:
Setting $\ makes newline after print happen. The pack unpack trick is at the heart of the method. The substitution trims zeros.{ local $\ = $/; for (@ARGV) { $_ = unpack 'B*', pack 'N', $_; s/^(?:0{8})*([01]{8,})/$1/g; print; } }
Update: majin, there is a limit of 32 bits. The substitution s/0{8}//g was meant to remove leading groups of eight zeros, but was incorrect, it will remove any group of eight zeros. Corrected in code. We match groups of eight zeros from the left end, then capture the rest, substituting the captured part.
After Compline,
Zaxo
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: int2bin
by majin (Acolyte) on Aug 11, 2002 at 17:47 UTC |