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

Re: One-liner ascii/bin

by Rolf (Novice)
on Nov 12, 2001 at 16:42 UTC ( #124813=note: print w/replies, xml ) Need Help??

in reply to One-liner ascii/bin

I am sorry I am not able to express myself better. What I am trying to explain is this: I write something like Perl -e ... "Rolf" and it comes out like this: 01010010011011110110110001100110 and if I use the 01's as argument in another one-liner I get the output: Rolf Thank you for your efforts and sorry about me not explaining it better Regards Rolf

Replies are listed 'Best First'.
Re: Re: One-liner ascii/bin
by blakem (Monsignor) on Nov 12, 2001 at 17:11 UTC
    All the pieces were there in the thread. I simply rearranged them...
    #!/usr/bin/perl -wT use strict; my $bin = ascii2bin('Rolf'); my $ascii = bin2ascii($bin); print "bin => $bin\n"; print "ascii => $ascii\n"; sub ascii2bin { my $ascii = shift; my $bin = join '', map {sprintf "%08b", ord($_)} split //, $ascii; return $bin; } sub bin2ascii { my $bin = shift; my $ascii = join '', map {chr(oct"0b$_")} $bin =~ /(\d{8})/g; return $ascii; } =OUTPUT bin => 01010010011011110110110001100110 ascii => Rolf

    Or, as one-liners.....

    perl -le 'print join"",map{sprintf"%08b",ord($_)}split//,pop' Rolf perl -le 'print join"",map{chr(oct"0b$_")}pop=~/(\d{8})/g' 01010010011 +011110110110001100110


      perl -e 'print unpack"B*",shift' Rolf
      perl -e 'print pack"B*",shift' 01010010011011110110110001100110
        Nice. For some reason I thought this approach had been suggested and rejected by Rolf but I misunderstood which part he objected to. These are much cleaner than the ones I came up with above.


        Hi, You are so veeery close, only that I get: Can't find string terminator "'" anywhere before EOF at -e line 1.
      Hi, You are getting a lot closer now. (With the last two onliners.) I get a "Can't find string terminator "'" anywhere before EOF at -e line 1." when trying them. But they do look a bit like the ones I lost. Regards Rolf
        I double checked them and they work fine for me under perl 5.6.1 and unix. I did a quick test with perl 5.05_003 and they didn't work, but I didn't get the error message you're seeing. (looks like sprintf got smarter in the newer perls)


        Hi, I use perl, v5.6.1 built for MSWin32-x86-multi-thread Regards Rolf

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://124813]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-10-04 04:21 GMT
Find Nodes?
    Voting Booth?

    No recent polls found