Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Convert potion of string to Octal

Reply
Thread Tools

Convert potion of string to Octal

 
 
Bill H
Guest
Posts: n/a
 
      01-03-2006
If I have a string that may contain characters with an ascii value of
128 or greater, other than looking at each character, is there a way of
converting each occurance of these characters to an octal
representation in the format of \### where ### is the octal value (the
slash is required in the output)?

For example, if I have a string that is:

"The next character is octal "

I want to convert it to the following format:

"The next character is octal \374"

If there is something in the perldocs on how to do this please let me
know where to look

Thank you,

Bill H

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      01-03-2006
Bill H wrote:
> If I have a string that may contain characters with an ascii value of
> 128 or greater, other than looking at each character, is there a way of
> converting each occurance of these characters to an octal
> representation in the format of \### where ### is the octal value (the
> slash is required in the output)?
>
> For example, if I have a string that is:
>
> "The next character is octal "
>
> I want to convert it to the following format:
>
> "The next character is octal \374"
>
> If there is something in the perldocs on how to do this please let me
> know where to look



s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-03-2006
Bill H wrote:
> If I have a string that may contain characters with an ascii value of
> 128 or greater,


I believe that's a contradiction in terms. ASCII goes from 0 to 127
(0x00 to 0x7F).

> other than looking at each character, is there a way of
> converting each occurance of these characters to an octal
> representation in the format of \### where ### is the octal value (the
> slash is required in the output)?
>
> For example, if I have a string that is:
>
> "The next character is octal "
>
> I want to convert it to the following format:
>
> "The next character is octal \374"
>
> If there is something in the perldocs on how to do this please let me
> know where to look


perldoc -f perlre
perldoc -f sprintf
perldoc -f ord

#!/usr/bin/perl -l
use strict;
use warnings;

$_ = 'This is my string';
s { ([\x80-\xFF]) }
{ sprintf '\\%o', ord($1) }gex;
print;
__END__

This is my \374 string


Paul Lalli

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-03-2006
Paul Lalli wrote:
> perldoc -f perlre


Er, no -f in that one. Just:
perldoc perlre

Paul Lalli

 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      01-03-2006

John W. Krahn wrote:
> Bill H wrote:
> > If I have a string that may contain characters with an ascii value of
> > 128 or greater, other than looking at each character, is there a way of
> > converting each occurance of these characters to an octal
> > representation in the format of \### where ### is the octal value (the
> > slash is required in the output)?
> >
> > For example, if I have a string that is:
> >
> > "The next character is octal "
> >
> > I want to convert it to the following format:
> >
> > "The next character is octal \374"
> >
> > If there is something in the perldocs on how to do this please let me
> > know where to look

>
>
> s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
>


Thanks for the quick response but that converts all the characters - is
there way for it to only do those with ascii values over 127?

I did a quick test of it using:

$a = "The next character is octal " ;
$a =~ s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
print $a;

Bill H

>
> John
> --
> use Perl;
> program
> fulfillment


 
Reply With Quote
 
brian d foy
Guest
Posts: n/a
 
      01-03-2006
In article <(E-Mail Removed). com>, Bill
H <(E-Mail Removed)> wrote:

> For example, if I have a string that is:
>
> "The next character is octal "
>
> I want to convert it to the following format:
>
> "The next character is octal \374"


The sprintf and printf have the %o format for octal numbers,
and you can use the ord() builtin to get the numeric value of
the character.

printf "The next character is octal \\%o", ord( $char );
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-03-2006
Bill H wrote:
> John W. Krahn wrote:
> > Bill H wrote:
> > s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> >

>
> Thanks for the quick response but that converts all the characters - is
> there way for it to only do those with ascii values over 127?
>
> I did a quick test of it using:
>
> $a = "The next character is octal " ;
> $a =~ s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> print $a;


You did something wrong. When I run those three lines of code - by
copy and pasting your post - I get:
The next character is octal \374

What output did you obtain?

Paul Lalli

 
Reply With Quote
 
brian d foy
Guest
Posts: n/a
 
      01-03-2006
In article <(E-Mail Removed) .com>,
Bill H <(E-Mail Removed)> wrote:

> John W. Krahn wrote:


> > s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;


> Thanks for the quick response but that converts all the characters - is
> there way for it to only do those with ascii values over 127?


> I did a quick test of it using:
>
> $a = "The next character is octal " ;
> $a =~ s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> print $a;


That script works for me. The character class is negated and excludes
all of the ASCII characters, so the substitution only works on the
non-ASCII ones.
 
Reply With Quote
 
Bill H
Guest
Posts: n/a
 
      01-03-2006

Paul Lalli wrote:
> Bill H wrote:
> > John W. Krahn wrote:
> > > Bill H wrote:
> > > s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> > >

> >
> > Thanks for the quick response but that converts all the characters - is
> > there way for it to only do those with ascii values over 127?
> >
> > I did a quick test of it using:
> >
> > $a = "The next character is octal " ;
> > $a =~ s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> > print $a;

>
> You did something wrong. When I run those three lines of code - by
> copy and pasting your post - I get:
> The next character is octal \374
>
> What output did you obtain?


Every character was converted to the \octal value. I replaced the
^[:ascii:] with the part of the code you gave [\x80-\xFF] and it worked
fine after that.

>
> Paul Lalli


 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      01-03-2006

Bill H wrote:
> Paul Lalli wrote:
> > Bill H wrote:
> > > ...
> > > > s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> > > >
> > >
> > > Thanks for the quick response but that converts all the characters - is
> > > there way for it to only do those with ascii values over 127?
> > >
> > > I did a quick test of it using:
> > >
> > > $a = "The next character is octal " ;
> > > $a =~ s/ ( [^[:ascii:]] ) / sprintf '\%o', ord $1 /gex;
> > > print $a;

> >
> > You did something wrong. When I run those three lines of code - by
> > copy and pasting your post - I get:
> > The next character is octal \374
> >
> > What output did you obtain?

>
> Every character was converted to the \octal value. I replaced the
> ^[:ascii:] with the part of the code you gave [\x80-\xFF] and it worked
> fine after that.


Your expression looks ok but you'd see a mass conversion if you
inserted
a space between the [ and ^ like this:

s/ ( [ ^[:ascii:] ] ) / sprintf '\%o', ord $1 /gex;
|
|
space

The regex 'x' modifier ignores (most) whitespace but not within a
character
class.

hth,
--
Charles DeRykus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Efficiently convert characters to octal representation Worky Workerson Perl Misc 11 07-29-2006 03:02 AM
String to Octal fuch6921@kettering.edu C Programming 27 02-24-2006 01:23 AM
How to convert a string like '777' to an octal integer like 0777? KB Python 5 07-31-2005 11:05 AM
converting characters to octal Hostos Java 7 10-15-2003 06:07 AM
strange replacement of . and , with octal counterpart Kermit Lowry Perl 0 09-25-2003 11:04 AM



Advertisments