Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Regex converting Binary to ASCII

by growlf (Pilgrim)
on Mar 19, 2002 at 22:48 UTC ( [id://152877]=perlquestion: print w/replies, xml ) Need Help??

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

Hi folx.

Got an odd one that I am sure is simple - but all the reading of http://www.perldoc.com/perl5.6.1/pod/perlre.html does not clear it up for me.

I am helping a comrade out, and what he wants to do is take a given string that has some ASCII codes in it, convert them to ASCII characters and hand it back.  I thought I could do this in a single regex - but so far I seem to be missing something.  I also need to state that the ASCII values are in decimal - not hex, wide hex, or oct - and this further confused my as to a solution.

Thank you for any help you give on this.



*G*

Replies are listed 'Best First'.
Re: Regex converting Binary to ASCII
by jmcnamara (Monsignor) on Mar 19, 2002 at 23:31 UTC

    Maybe something like this?:
    #!/usr/bin/perl -wl use strict; my $str = 'The ASCII chars 64 and 35'; print $str; $str =~ s/(\d+)/chr $1/eg; print $str; __END__ Prints: The ASCII chars 64 and 35 The ASCII chars @ and #

    --
    John.

      You could even add some error checking as ASCII requires that our number be between 0-127

      $str = "Only change valid codes 128 -42 33\n"; print $str; $str =~ s/([+-]?\d+)/ ($1 > 0 and $1 < 128) ? chr $1 : $1 /eg; print $str;

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      That was it! thanks!

      *G*
Re: Regex converting Binary to ASCII
by lestrrat (Deacon) on Mar 19, 2002 at 22:59 UTC

    How are the codes embedded? are they always 3 digit codes ( i.e. 000-255 )? In order to capture the codes, you need some sort of pattern.... which you didn't state

    once you capture the code, you can then just use chr

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-19 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found