Convert potion of string to Octal

Discussion in 'Perl Misc' started by Bill H, Jan 3, 2006.

  1. Bill H

    Bill H Guest

    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
     
    Bill H, Jan 3, 2006
    #1
    1. Advertising

  2. 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
     
    John W. Krahn, Jan 3, 2006
    #2
    1. Advertising

  3. Bill H

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 3, 2006
    #3
  4. Bill H

    Paul Lalli Guest

    Paul Lalli wrote:
    > perldoc -f perlre


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

    Paul Lalli
     
    Paul Lalli, Jan 3, 2006
    #4
  5. Bill H

    Bill H Guest

    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
     
    Bill H, Jan 3, 2006
    #5
  6. Bill H

    brian d foy Guest

    In article <>, Bill
    H <> 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 );
     
    brian d foy, Jan 3, 2006
    #6
  7. Bill H

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 3, 2006
    #7
  8. Bill H

    brian d foy Guest

    In article <>,
    Bill H <> 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.
     
    brian d foy, Jan 3, 2006
    #8
  9. Bill H

    Bill H Guest

    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
     
    Bill H, Jan 3, 2006
    #9
  10. Bill H

    Guest

    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
     
    , Jan 3, 2006
    #10
  11. Bill H

    Bill H Guest

    wrote:
    > 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.
    >


    There was a space in it - so that would explain it.

    > hth,
    > --
    > Charles DeRykus
     
    Bill H, Jan 4, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kermit Lowry
    Replies:
    0
    Views:
    376
    Kermit Lowry
    Sep 25, 2003
  2. Hostos
    Replies:
    7
    Views:
    5,237
    La'ie Techie
    Oct 15, 2003
  3. KB
    Replies:
    5
    Views:
    772
    Steven D'Aprano
    Jul 31, 2005
  4. String to Octal

    , Feb 21, 2006, in forum: C Programming
    Replies:
    27
    Views:
    1,077
    Flash Gordon
    Feb 24, 2006
  5. Worky Workerson
    Replies:
    11
    Views:
    339
    Ben Morrow
    Jul 29, 2006
Loading...

Share This Page