Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

decimal -> Binary

3 views
Skip to first unread message

Sandro CAZZANIGA

unread,
Nov 16, 2011, 8:26:39 AM11/16/11
to f...@perl.org
hi!

Just a little JAPH for convert decimal to binary.... Feel free to comment
it ;)
----------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
eval {@ARGV} or die("No args") ;
foreach my $arg(@ARGV) {
my $convert = unpack("B*",pack("N", $arg));
my @convertsp = split("", $convert);
my $count = 0;
foreach (@convertsp) {
unless ($convertsp[$count] == 1) {
$convertsp[$count] = "" ;
$count++;
}
}
print("$arg: @convertsp \n");
}
exit 0;

Georg Moritz

unread,
Nov 16, 2011, 9:32:03 AM11/16/11
to Sandro CAZZANIGA, f...@perl.org
Greetings Sandro & all,

From the keyboard of Sandro CAZZANIGA [16.11.11,14:26]:

> hi!
>
> Just a little JAPH for convert decimal to binary.... Feel free to comment
> it ;)

golfed down a bit, just for fun...

#!/usr/bin/perl -l
@ARGV or die "No args";
print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0+//?$_:$_ for@ARGV;

> ----------------------------------------------------------------
> #!/usr/bin/perl
> use strict;
> use warnings;
> eval {@ARGV} or die("No args") ;
> foreach my $arg(@ARGV) {
> my $convert = unpack("B*",pack("N", $arg));
> my @convertsp = split("", $convert);
> my $count = 0;
> foreach (@convertsp) {
> unless ($convertsp[$count] == 1) {
> $convertsp[$count] = "" ;
> $count++;
> }
> }
> print("$arg: @convertsp \n");
> }
> exit 0;
>

cheers,
0--gg-

--
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

John Douglas Porter

unread,
Nov 16, 2011, 9:32:35 AM11/16/11
to f...@perl.org

What's the point? Seems to me the only purpose of this code beyond the obvious

unpack("B*",pack("N", $arg))

line is to reformat the result into an odd, probably application-dependent format.

--- On Wed, 11/16/11, Sandro CAZZANIGA <cazzanig...@gmail.com> wrote:

Ronald J Kimball

unread,
Nov 16, 2011, 9:57:34 AM11/16/11
to Georg Moritz, Sandro CAZZANIGA, f...@perl.org
On Wed, Nov 16, 2011 at 03:32:03PM +0100, Georg Moritz wrote:
> golfed down a bit, just for fun...
>
> #!/usr/bin/perl -l
> @ARGV or die "No args";
> print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0+//?$_:$_ for@ARGV;

That should be s/0*//, otherwise you'll get the wrong result for numbers
above 2147483647.

Ronald

P.S. Golfed further:
#!/usr/bin/perl -l
print"$_: ",(unpack"B*",pack N,$_)=~/(1.*)/for@ARGV?@ARGV:die"No args"

Olof Johansson

unread,
Nov 16, 2011, 10:14:57 AM11/16/11
to Ronald J Kimball, f...@perl.org
On 2011-11-16 09:57 -0500, Ronald J Kimball wrote:
> That should be s/0*//, otherwise you'll get the wrong result for numbers
> above 2147483647.

That should be s/^0*//, otherwise you'll get the wrong result for
numbers above 2147483647.

Also:
printf"$_:%b\n",$_ for@ARGV

--
---------------------------------------------------------------
| Olof Johansson http://stdlib.se/ |
---------------------------------------------------------------

Ronald J Kimball

unread,
Nov 16, 2011, 11:57:50 AM11/16/11
to Olof Johansson, f...@perl.org
On Wed, Nov 16, 2011 at 04:14:57PM +0100, Olof Johansson wrote:
> On 2011-11-16 09:57 -0500, Ronald J Kimball wrote:
> > That should be s/0*//, otherwise you'll get the wrong result for numbers
> > above 2147483647.
>
> That should be s/^0*//, otherwise you'll get the wrong result for
> numbers above 2147483647.

No, you are wrong. s/0*// is sufficient, because /0*/ will always match at
the start of the string anyway.

Ronald

Olof Johansson

unread,
Nov 16, 2011, 1:49:14 PM11/16/11
to Ronald J Kimball, f...@perl.org
On 2011-11-16 11:57 -0500, Ronald J Kimball wrote:
> No, you are wrong. s/0*// is sufficient, because /0*/ will always match at
> the start of the string anyway.

You're clearly an expert. I yield. Can you open a bug report with perl
and getting this fixed?

Daniel Cutter

unread,
Nov 16, 2011, 1:49:12 PM11/16/11
to f...@perl.org
Hasn't anyone noticed that a decimal to binary or whatever conversion
isn't a JAPH by definition, alone because it does anything other than
output the JAPH? With a looser definition it must at least output "Just
another Perl hacker," perhaps on stderr

Daniel Cutter
@_=($_="aaceeehhjklnoprrrsttu", q<)411!**.&#)(#'&$!!"!!>);for$a(map{
'.'x(ord($_)-3*11)}$_[1]=~m,.,g){s,($a)(.),$1,,$b.=$2};$c.="(.{$_})"
for(4,7,4,6);@_=$b=~m,$c,;push@_,"\n";print map{ucfirst}map{$_.$"}@_

Ronald J Kimball

unread,
Nov 16, 2011, 2:14:09 PM11/16/11
to Olof Johansson, f...@perl.org
On Wed, Nov 16, 2011 at 07:49:14PM +0100, Olof Johansson wrote:
> On 2011-11-16 11:57 -0500, Ronald J Kimball wrote:
> > No, you are wrong. s/0*// is sufficient, because /0*/ will always match at
> > the start of the string anyway.
>
> You're clearly an expert. I yield. Can you open a bug report with perl
> and getting this fixed?

It's working as expected for me, so I'm not sure what needs to be fixed.

% cat tmp.pl
#!perl -l
print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//?$_:$_ for@_=@ARGV;
print"$_: ",($_=unpack"B*",pack"N",$_)=~s/^0*//?$_:$_ for@_=@ARGV;
% perl tmp.pl 2147483648 3000000000
2147483648: 10000000000000000000000000000000
3000000000: 10110010110100000101111000000000
2147483648: 10000000000000000000000000000000
3000000000: 10110010110100000101111000000000
%

Are you seeing different behavior?

Ronald

Georg Moritz

unread,
Nov 16, 2011, 2:35:02 PM11/16/11
to f...@perl.org
From the keyboard of Ronald J Kimball [16.11.11,14:14]:

> On Wed, Nov 16, 2011 at 07:49:14PM +0100, Olof Johansson wrote:
>> On 2011-11-16 11:57 -0500, Ronald J Kimball wrote:
>>> No, you are wrong. s/0*// is sufficient, because /0*/ will always match at
>>> the start of the string anyway.

rye

>> You're clearly an expert. I yield. Can you open a bug report with perl
>> and getting this fixed?
>
> It's working as expected for me, so I'm not sure what needs to be fixed.

well... the ternary ?: isn't necessary with s/0*// because s/0*// is
always successful, so

print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//&&$_ for@ARGV;

two strokes ;-)
but as Olof pointed out,

printf"$_:%b\n",$_ for@ARGV

is less convoluted. Why do I forget about printf almost always?
'cause I'm not a C hacker, but a heck parler, I guess ;-)

for some JAPH, see my signature. Latin-1 only, though

cheers,
0--gg-

> % cat tmp.pl
> #!perl -l
> print"$_: ",($_=unpack"B*",pack"N",$_)=~s/0*//?$_:$_ for@_=@ARGV;
> print"$_: ",($_=unpack"B*",pack"N",$_)=~s/^0*//?$_:$_ for@_=@ARGV;
> % perl tmp.pl 2147483648 3000000000
> 2147483648: 10000000000000000000000000000000
> 3000000000: 10110010110100000101111000000000
> 2147483648: 10000000000000000000000000000000
> 3000000000: 10110010110100000101111000000000
> %
>
> Are you seeing different behavior?
>
> Ronald
>

Olof Johansson

unread,
Nov 16, 2011, 3:34:58 PM11/16/11
to Ronald J Kimball, f...@perl.org
On 2011-11-16 14:14 -0500, Ronald J Kimball wrote:
> It's working as expected for me, so I'm not sure what needs to be fixed.

Hum, now I'm ashamed. Sorry. But why is that not greedy? (I got other
results earlier, but I can't reproduce now so I have probably made an
error in my earlier attempts.)

John Douglas Porter

unread,
Nov 16, 2011, 3:40:57 PM11/16/11
to Ronald J Kimball, Olof Johansson, f...@perl.org

Olof Johansson <ol...@ethup.se> wrote:
> But why is that not greedy?

Remember, the *first* match wins, even if it's shorter
than a possible later match.

ISTR that some have argued that's a bug.

Well, too bad. It's too late. :-)

--
john "many jars" porter

Ronald J Kimball

unread,
Nov 16, 2011, 3:46:52 PM11/16/11
to Olof Johansson, f...@perl.org
On Wed, Nov 16, 2011 at 09:34:58PM +0100, Olof Johansson wrote:
> On 2011-11-16 14:14 -0500, Ronald J Kimball wrote:
> > It's working as expected for me, so I'm not sure what needs to be fixed.
>
> Hum, now I'm ashamed. Sorry. But why is that not greedy? (I got other
> results earlier, but I can't reproduce now so I have probably made an
> error in my earlier attempts.)

It is greedy, but the important thing to remember is that the regular
expression engine will find the longest *leftmost* match.

For example, "00111000" =~ /0*/ will match "(00)111000". It won't match
"(0)0111000", because, although it's a leftmost match, it's not the longest
leftmost match. And it won't match "00111(000)" because that's not a
leftmost match, regardless of the length.

Ronald

Aristotle Pagaltzis

unread,
Nov 18, 2011, 3:41:43 PM11/18/11
to f...@perl.org
* Ronald J Kimball <rjk-pe...@tamias.net> [2011-11-16 21:50]:
> It is greedy, but the important thing to remember is that the regular
> expression engine will find the longest *leftmost* match.

To put that a third way: the engine will match at the first possible
location, and will make the match as long as possible at that location.
0 new messages