char->integer, integer->char commands

Discussion in 'Perl Misc' started by christophergraber@gmail.com, Nov 21, 2005.

  1. Guest

    Hi,

    I created two subroutines, I want to know if there is a better, faster
    way to do this. I just use ord() and chr():

    1) Take a upper case string like "GRABER" and covert it to its ASCII
    equivalent (a string of 2 digit numbers, concatenated) - 718265666982

    # Convert a string of A-Z Characters to 2 digit ASCII numbers, one
    after another
    sub ToNumbers {
    my $character_string = shift;
    # remove whitespace at beginning and end of name?
    # what about spaces or dashe's IN the name?
    my $numbers_string;

    for ($i=0; $i < length($character_string); $i++) {
    $numbers_string = $numbers_string . ord(substr($character_string,
    $i, 1));
    }

    return $numbers_string;
    }

    2) Take a string like "718265666982" and convert it to a string of
    characters like "GRABER"

    # Converts a string of two digit ASCII codes to a string of characters
    sub ToCharacters {
    my $number_string = shift;
    # remove whitespace at beginning and end of numbers?
    # what about spaces or anything else in the numbers?
    my $characters_string;

    for ($i=0; $i < length($number_string); $i=$i+2) {
    print "i is $i \n";
    $characters_string = $characters_string .
    chr(substr($number_string, $i, 2));
    }

    return $characters_string;
    }
     
    , Nov 21, 2005
    #1
    1. Advertising

  2. wrote in news:1132597484.172306.280740
    @f14g2000cwb.googlegroups.com:

    > I created two subroutines, I want to know if there is a better, faster
    > way to do this. I just use ord() and chr():
    >
    > 1) Take a upper case string like "GRABER" and covert it to its ASCII
    > equivalent (a string of 2 digit numbers, concatenated) - 718265666982


    Are those hex digits?

    >
    > # Convert a string of A-Z Characters to 2 digit ASCII numbers, one
    > after another
    > sub ToNumbers {


    What you have here is an attempt to write C in Perl.

    #!/usr/bin/perl

    use strict;
    use warnings;

    my $s = 'GRABER';
    $s =~ s/([A-Z])/sprintf '%2.2X', ord $1/ge;

    print "$s\n";

    $s =~ s/([[:xdigit:]]{2})/chr hex $1/ge;
    print "$s\n";

    __END__

    D:\Home\asu1\UseNet\clpmisc> c
    475241424552
    GRABER

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Nov 21, 2005
    #2
    1. Advertising

  3. Joel Graber Guest

    writes:
    > I created two subroutines, I want to know if there is a better, faster
    > way to do this. I just use ord() and chr():
    >
    > 1) Take a upper case string like "GRABER" and covert it to its ASCII
    > equivalent (a string of 2 digit numbers, concatenated) - 718265666982
    >
    > 2) Take a string like "718265666982" and convert it to a string of
    > characters like "GRABER"


    Perl is faster when iteration is implicit.
    s/(.)/f($1)/g; # iterates by character nicely.

    #!/usr/local/bin/perl
    use warnings;
    use strict;
    my (%H,%h);
    %H = reverse (%h=qw( G 71 R 82 A 65 B 66 E 69));
    $_ = "GRABER\n";
    print;
    s/(.)/$h{$1}/g; # string to ASCII concatenated
    print;
    s/(..)/$H{$1}/g; # ASCII concat string to ASCII
    print;

    # prints
    #GRABER
    #718265666982
    #GRABER
    Note that dot (.) did not match \n at end of input line

    These also work, for the given input string.
    s/(.)/ord($1)/ge; # string to ASCII concatenated
    s/(..)/chr($1)/ge; # and back

    Perl is faster when iteration is implicit.

    --
    Joel
     
    Joel Graber, Nov 21, 2005
    #3
  4. Also sprach :

    > I created two subroutines, I want to know if there is a better, faster
    > way to do this. I just use ord() and chr():
    >
    > 1) Take a upper case string like "GRABER" and covert it to its ASCII
    > equivalent (a string of 2 digit numbers, concatenated) - 718265666982


    Use unpack with the 'C' template. Your ToNumbers function then becomes:

    sub ToNumbers {
    unpack "C*", shift;
    }

    > # Convert a string of A-Z Characters to 2 digit ASCII numbers, one
    > after another
    > sub ToNumbers {
    > my $character_string = shift;
    > # remove whitespace at beginning and end of name?
    > # what about spaces or dashe's IN the name?
    > my $numbers_string;
    >
    > for ($i=0; $i < length($character_string); $i++) {
    > $numbers_string = $numbers_string . ord(substr($character_string,
    > $i, 1));
    > }
    >
    > return $numbers_string;
    > }
    >
    > 2) Take a string like "718265666982" and convert it to a string of
    > characters like "GRABER"


    Use pack:

    sub ToCharactes {
    pack "C*", shift;
    }

    See `perldoc -f pack` for a list of the various available templates.
    There's also a tutorial on pack worth reading as pack/unpack can
    sometimes be tricky to use correctly: `perldoc perlpacktut`.

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Nov 22, 2005
    #4
  5. Tassilo v. Parseval wrote:
    > Also sprach :
    >
    >>I created two subroutines, I want to know if there is a better, faster
    >>way to do this. I just use ord() and chr():
    >>
    >>1) Take a upper case string like "GRABER" and covert it to its ASCII
    >>equivalent (a string of 2 digit numbers, concatenated) - 718265666982

    >
    > Use unpack with the 'C' template. Your ToNumbers function then becomes:
    >
    > sub ToNumbers {
    > unpack "C*", shift;
    > }


    That doesn't create a string of numbers, unpack() returns a list.

    sub ToNumbers {
    pack '(a2)*', unpack 'C*', shift;
    }


    >># Convert a string of A-Z Characters to 2 digit ASCII numbers, one
    >>after another
    >>sub ToNumbers {
    >> my $character_string = shift;
    >> # remove whitespace at beginning and end of name?
    >> # what about spaces or dashe's IN the name?
    >> my $numbers_string;
    >>
    >> for ($i=0; $i < length($character_string); $i++) {
    >> $numbers_string = $numbers_string . ord(substr($character_string,
    >>$i, 1));
    >> }
    >>
    >> return $numbers_string;
    >>}
    >>
    >>2) Take a string like "718265666982" and convert it to a string of
    >>characters like "GRABER"

    >
    > Use pack:
    >
    > sub ToCharactes {
    > pack "C*", shift;
    > }


    sub ToCharactes {
    pack 'C*', unpack '(a2)*', shift;
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Nov 22, 2005
    #5
  6. Also sprach John W. Krahn:
    > Tassilo v. Parseval wrote:
    >> Also sprach :
    >>
    >>>I created two subroutines, I want to know if there is a better, faster
    >>>way to do this. I just use ord() and chr():
    >>>
    >>>1) Take a upper case string like "GRABER" and covert it to its ASCII
    >>>equivalent (a string of 2 digit numbers, concatenated) - 718265666982

    >>
    >> Use unpack with the 'C' template. Your ToNumbers function then becomes:
    >>
    >> sub ToNumbers {
    >> unpack "C*", shift;
    >> }

    >
    > That doesn't create a string of numbers, unpack() returns a list.
    >
    > sub ToNumbers {
    > pack '(a2)*', unpack 'C*', shift;
    > }


    [...]

    >>>2) Take a string like "718265666982" and convert it to a string of
    >>>characters like "GRABER"

    >>
    >> Use pack:
    >>
    >> sub ToCharactes {
    >> pack "C*", shift;
    >> }

    >
    > sub ToCharactes {
    > pack 'C*', unpack '(a2)*', shift;
    > }


    You're right, sorry for the sloppy posting. I essentially only tested
    things with 'print' and it worked (because it prints lists naturally).

    Tassilo
    --
    use bigint;
    $n=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Nov 22, 2005
    #6
    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. wwj
    Replies:
    7
    Views:
    561
  2. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,981
    Thomas Stegen
    Jun 28, 2003
  3. wwj
    Replies:
    24
    Views:
    2,525
    Mike Wahler
    Nov 7, 2003
  4. Tim Stanka
    Replies:
    1
    Views:
    810
    Jeff Epler
    Aug 2, 2004
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,071
    Ian Collins
    May 9, 2006
Loading...

Share This Page