Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Obfuscation or printing words like \160\162\151\156\164

by jbrugger (Parson)
on Apr 06, 2005 at 04:55 UTC ( [id://445177]=perlquestion: print w/replies, xml ) Need Help??

jbrugger has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

Once i came across a JAPH tutorial (i can't find it anymore in the supersearch), and i found out that you can write the command print as \160\162\151\156\164

Now this opened my eyes in JAPH creation (i'm a rookie in it), and is wanted to list the total alphabet.
I was amazed that there is no logic in numbering: \101=A, \102=B .... \108=8  \109=9 \110=H

So i thought, let's print a list from 100 to 200, so i can read back the output, and i started:

for (my $i=101; $i<=200; $i++) { $a = "\\" . $i; print "$i = $a \n"; }
As most of the JAPHERS probably know, this prints:
101 = \101
102 = \102
etc...
I know i'm lazy, (thats why i like perl), and i don't want to write out all the chars by hand, but i would not know how to proceed.

Could any monk enlighten my darkend path?

Thanks!

"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Replies are listed 'Best First'.
Re: Obfuscation or printing words like \160\162\151\156\164
by ikegami (Patriarch) on Apr 06, 2005 at 05:04 UTC
    for (my $i=33; $i<=126; $i++) { printf("\\%o = %s\n", $i, chr($i)); }

    Output:

    There's no 108 and 109 in octal.

    \40 gives character 32, the space.

      Oké. Thanks that worked. Perhaps a stupid question, but where could i have found this in documentation?
      I've used supersearch, google, but i came across nothing.
      perhaps a bad search query...

      *** UPDATE *** Well, i made a little script to make it more easy to translate your text to create obfuscated code... Feel free to enhance and critisize, but thanks to you all for the help:
      #!/usr/bin/perl -w use strict; my $chars = {}; for (my $i=33; $i<=127; $i++) { my $oct = sprintf("\\%o", $i); $chars->{chr($i)} = $oct; } foreach my $word (@ARGV) { my(@str) = split(//,$word); foreach my $letter (@str) { print $chars->{$letter}; } print " "; } # ./obus.pl just another Perl hacker, # returns # \152\165\163\164 \141\156\157\164\150\145\162 \120\145\162\154 \150\ +141\143\153\145\162\54

      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

        String literals are documented in the section "Quote and Quote-like Operators" of perlop.

        printf format strings are documented under sprintf in perlfunc, as indicated under printf in perlfunc.

        As stated by the previous poster, the \102 \103 etc are octal codes. These can be looked up in an ascii table such as: this one.

        Also as stated, since it is octal, it is a base 8 system, meaning the numbering goes:

        000 001 002 003 004 005 006 007
        010 011 012 013 014 015 016 017
        020 021 022 023 024 025 026 027
        etc
        etc
        where could i have found this in documentation?

        Any ASCII table. And as for them not seeming to be in any order, I think it's actually quite orderly, so long as you stay in octal:

        • Uppercase : 0100 + letter position
        • Lowercase : 0140 + letter position
        • Digits : 060 + number
Re: Obfuscation or printing words like \160\162\151\156\164
by Tanktalus (Canon) on Apr 06, 2005 at 15:02 UTC

    So far, everyone has described the "enlightened" way to do this. As in, they read your question, and came to the conclusion that you asked "how do I do X using Y?" and so they were telling you how to do X using X.

    I'll go the other route this time. Let's say I haven't been enlightened (for whatever reason, the thought that these were octal didn't occur to me until it was pointed out, so we're not far from the truth here ;->). I would do the following:

    use strict; for my $i (101 .. 200) { my $a = '\\' . $i; my $val = eval qq{"$a"} || $@; print "$i = $val\n"; }
    This answers your question on how to get perl to write out all the characters for you. But it does seem a bit funny. So I'd go the next step:
    use strict; my %data; for my $i (101 .. 200) { my $a = '\\' . $i; my $val = eval qq{"$a"} || $@; #print "$i = $val\n"; $data{$i} = $val; } use Data::Dumper; $Data::Dumper::Sortkeys = sub { [ sort { $a <=> $b } keys %{+shift} ] }; $Data::Dumper::Useqq = 1; print Dumper(\%data);
    Now we're getting somewhere interesting. For example, I see that "199 => "\00199"," appears. And yet, I also see "177 => "\177",". At this point, I'd look up the documentation and, hopefully, find out what this really means, as is described by everyone else.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://445177]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found