adding ascii values in string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil W.

    adding ascii values in string

    I'm coming back to Perl after a long absence. I am trying to add up the
    ascii values of the characters in a string. Can anyone refresh my memory?

    Thanks.


  • Roel van der Steen

    #2
    Re: adding ascii values in string

    On Mon, 15 Mar 2004 at 16:39 GMT, Neil W. wrote:[color=blue]
    > I'm coming back to Perl after a long absence. I am trying to add up the
    > ascii values of the characters in a string. Can anyone refresh my memory?
    >
    > Thanks.
    >
    >[/color]
    That would be (untested):

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

    my $chars = 'abcdef123';
    my $ord_sum;

    $ord_sum += ord $_ foreach split //, $chars;

    print $ord_sum;

    __END__

    prints: 747

    Comment

    • Joe Smith

      #3
      Re: adding ascii values in string

      Neil W. wrote:
      [color=blue]
      > I'm coming back to Perl after a long absence. I am trying to add up the
      > ascii values of the characters in a string. Can anyone refresh my memory?[/color]

      perldoc -f unpack

      Note where it documents how to use the unpack() function to calculate
      checksums, such as to emulate the System V 'sum' program.
      -Joe

      Comment

      • Robert

        #4
        Re: adding ascii values in string

        Thanks! Will test it out. - Neil

        ---------------------------------

        "Roel van der Steen" <roel-perl@st2x.net> wrote in message
        news:slrnc5bo83 .r60.roel-perl@localhost. localdomain...
        On Mon, 15 Mar 2004 at 16:39 GMT, Neil W. wrote:[color=blue]
        > I'm coming back to Perl after a long absence. I am trying to add up the
        > ascii values of the characters in a string. Can anyone refresh my memory?
        >
        > Thanks.
        >
        >[/color]
        That would be (untested):

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

        my $chars = 'abcdef123';
        my $ord_sum;

        $ord_sum += ord $_ foreach split //, $chars;

        print $ord_sum;

        __END__

        prints: 747


        Comment

        • Robert

          #5
          Re: adding ascii values in string

          Thanks for the tip. I'm doing a variation on a checksum. - Neil

          -------------------------------

          "Joe Smith" <Joe.Smith@inwa p.com> wrote in message
          news:5mp5c.1699 4$1p.303958@att bi_s54...
          Neil W. wrote:
          [color=blue]
          > I'm coming back to Perl after a long absence. I am trying to add up the
          > ascii values of the characters in a string. Can anyone refresh my memory?[/color]

          perldoc -f unpack

          Note where it documents how to use the unpack() function to calculate
          checksums, such as to emulate the System V 'sum' program.
          -Joe


          Comment

          Working...