convert char to byte representation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Philipp H. Mohr

    #1

    convert char to byte representation

    Hello,

    I am trying to xor the byte representation of every char in a string with
    its predecessor. But I don't know how to convert a char into its byte
    representation. This is to calculate the nmea checksum for gps data.

    e.g. everything between $ and * needs to be xor:
    $GPGSV,3,1,10,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,44*
    to get the checksum.


    Thank you for you help.

    Phil
  • Larry Bates

    #2
    Re: convert char to byte representation

    ord(c) gives you decimal representation of a character.

    -Larry Bates

    Philipp H. Mohr wrote:[color=blue]
    > Hello,
    >
    > I am trying to xor the byte representation of every char in a string with
    > its predecessor. But I don't know how to convert a char into its byte
    > representation. This is to calculate the nmea checksum for gps data.
    >
    > e.g. everything between $ and * needs to be xor:
    > $GPGSV,3,1,10,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,44*
    > to get the checksum.
    >
    >
    > Thank you for you help.
    >
    > Phil[/color]

    Comment

    • Scott David Daniels

      #3
      Re: convert char to byte representation

      Philipp H. Mohr wrote:[color=blue]
      > I am trying to xor the byte representation of every char in a string with
      > its predecessor. But I don't know how to convert a char into its byte
      > representation.[/color]
      ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the byte.
      [color=blue]
      > e.g. everything between $ and * needs to be xor:
      > $GPGSV,3,1,10,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,44*
      > to get the checksum.[/color]

      Probably you want a byte-array here, rather than going char-by-char.
      Try:
      import array
      base = ('$GPGSV,3,1,10 ,06,79,187,39,3 0,59,098,'
      '40,25,51,287,0 0,05,25,103,44* ')
      bytes = array.array('b' , base[1 : -1])
      for i in reversed(range( len(bytes))):
      bytes[i] ^= bytes[i-1]
      result = bytes.tostring( )

      --Scott David Daniels
      scott.daniels@a cm.org

      Comment

      • Grant Edwards

        #4
        Re: convert char to byte representation

        On 2005-10-10, Larry Bates <larry.bates@we bsafe.com> wrote:
        [color=blue][color=green]
        >> I am trying to xor the byte representation of every char in a string with
        >> its predecessor. But I don't know how to convert a char into its byte
        >> representation. This is to calculate the nmea checksum for gps data.[/color][/color]
        [color=blue]
        > ord(c) gives you decimal representation of a character.[/color]

        While ord(c) is what the OP needs, it doesn't give a decimal
        represention -- which I guess would be a string like "65" for
        the ASCII characer "A". What ord() gives you is an integer
        object with the value of the character [which the hardware
        stores in binary on all of the platforms I'm aware of].

        --
        Grant Edwards grante Yow! Hmmm... A hash-singer
        at and a cross-eyed guy were
        visi.com SLEEPING on a deserted
        island, when...

        Comment

        • Rick Wotnaz

          #5
          Re: convert char to byte representation

          Scott David Daniels <scott.daniels@ acm.org> wrote in
          news:434ab2f6$1 @nntp0.pdx.net:
          [color=blue]
          > Philipp H. Mohr wrote:[color=green]
          >> I am trying to xor the byte representation of every char in a
          >> string with its predecessor. But I don't know how to convert a
          >> char into its byte representation.[/color]
          > ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the
          > byte.
          >[color=green]
          >> e.g. everything between $ and * needs to be xor:
          >> $GPGSV,3,1,10,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,
          >> 103,44*
          >> to get the checksum.[/color]
          >
          > Probably you want a byte-array here, rather than going
          > char-by-char. Try:
          > import array
          > base = ('$GPGSV,3,1,10 ,06,79,187,39,3 0,59,098,'
          > '40,25,51,287,0 0,05,25,103,44* ')
          > bytes = array.array('b' , base[1 : -1])
          > for i in reversed(range( len(bytes))):
          > bytes[i] ^= bytes[i-1]
          > result = bytes.tostring( )
          >
          > --Scott David Daniels
          > scott.daniels@a cm.org
          >[/color]

          What is the byte representation of 287?

          --
          rzed

          Comment

          • Peter Otten

            #6
            Re: convert char to byte representation

            Scott David Daniels wrote:
            [color=blue]
            > Philipp H. Mohr wrote:[color=green]
            >> I am trying to xor the byte representation of every char in a string with
            >> its predecessor. But I don't know how to convert a char into its byte
            >> representation.[/color]
            > ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the byte.
            >[color=green]
            >> e.g. everything between $ and * needs to be xor:
            >> $GPGSV,3,1,10,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,44*
            >> to get the checksum.[/color]
            >
            > Probably you want a byte-array here, rather than going char-by-char.
            > Try:
            > import array
            > base = ('$GPGSV,3,1,10 ,06,79,187,39,3 0,59,098,'
            > '40,25,51,287,0 0,05,25,103,44* ')
            > bytes = array.array('b' , base[1 : -1])
            > for i in reversed(range( len(bytes))):
            > bytes[i] ^= bytes[i-1]
            > result = bytes.tostring( )[/color]

            Seems like the OP doesn't need what he asked for. The simpler

            def checksum(s):
            assert s[0] == "$"
            assert s[-1] == "*"
            result = 0
            for c in s[1:-1]:
            result ^= ord(c)
            return result

            should do.

            Peter

            Comment

            • Larry Bates

              #7
              Re: convert char to byte representation

              I've always read it written that the number that is returned by
              ord(c) is the "decimal" (not hex, not octal) representation of
              the ASCII/UNICODE character that is stored in memory location
              pointed to by variable c. While the result is an integer (as
              it couldn't really be anything else), I believe that most
              character charts list the number that ord() returns as the
              "decimal representation" of that character (as they normally
              also show the octal and hex values as well).

              Probably an "old school" answer on my part.

              -Larry Bates

              Grant Edwards wrote:[color=blue]
              > On 2005-10-10, Larry Bates <larry.bates@we bsafe.com> wrote:
              >
              >[color=green][color=darkred]
              >>>I am trying to xor the byte representation of every char in a string with
              >>>its predecessor. But I don't know how to convert a char into its byte
              >>>representati on. This is to calculate the nmea checksum for gps data.[/color][/color]
              >
              >[color=green]
              >>ord(c) gives you decimal representation of a character.[/color]
              >
              >
              > While ord(c) is what the OP needs, it doesn't give a decimal
              > represention -- which I guess would be a string like "65" for
              > the ASCII characer "A". What ord() gives you is an integer
              > object with the value of the character [which the hardware
              > stores in binary on all of the platforms I'm aware of].
              >[/color]

              Comment

              • Mike Meyer

                #8
                Re: convert char to byte representation

                [Format recovered from top posting.]

                Larry Bates <larry.bates@we bsafe.com> writes:[color=blue]
                > Grant Edwards wrote:[color=green]
                >> On 2005-10-10, Larry Bates <larry.bates@we bsafe.com> wrote:[color=darkred]
                >>>>I am trying to xor the byte representation of every char in a string with
                >>>>its predecessor. But I don't know how to convert a char into its byte
                >>>>representat ion. This is to calculate the nmea checksum for gps data.[/color]
                >>[color=darkred]
                >>>ord(c) gives you decimal representation of a character.[/color]
                >>
                >> While ord(c) is what the OP needs, it doesn't give a decimal
                >> represention -- which I guess would be a string like "65" for
                >> the ASCII characer "A". What ord() gives you is an integer
                >> object with the value of the character [which the hardware
                >> stores in binary on all of the platforms I'm aware of].[/color]
                >
                > I've always read it written that the number that is returned by
                > ord(c) is the "decimal" (not hex, not octal) representation of
                > the ASCII/UNICODE character that is stored in memory location
                > pointed to by variable c. While the result is an integer (as
                > it couldn't really be anything else), I believe that most
                > character charts list the number that ord() returns as the
                > "decimal representation" of that character (as they normally
                > also show the octal and hex values as well).[/color]

                The value returned by ord is a *number*. That number has a decimal
                representation. It also has a hex representation and an octal
                representation. These are all strings, and they all represent the same
                number. You can't print a number - you can only print characters. So
                Python (indeed, most languages) translate the number into a string of
                characters that represent the number, using the decimal
                representation. You can use the hex and oct builtins to ask for the
                hex and octal representations of that number and print those strings
                if you want.
                [color=blue]
                > Probably an "old school" answer on my part.[/color]

                The decimal representation of ' ' is '32'. Python doesn't think that
                that's what ord(' ') returns:
                [color=blue][color=green][color=darkred]
                >>> ord(' ') == '32'[/color][/color][/color]
                False

                So I'd say it was "wrong" rather than old school.

                On the other hand, if you check ord(' ') against numbers, it doeesn't
                care what representation you use, so long as they represent the same
                number:
                [color=blue][color=green][color=darkred]
                >>> ord(' ') == 0x20[/color][/color][/color]
                True[color=blue][color=green][color=darkred]
                >>> ord(' ') == 040[/color][/color][/color]
                True[color=blue][color=green][color=darkred]
                >>> ord(' ') == 32[/color][/color][/color]
                True

                Of course you can't read a number any more than you can write one, so
                Python kindly translates strings representing numbers into numbers
                when itt reads them. This process is often referred to as "reading a
                number", but what's actually read is characters.

                <mike
                --
                Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
                Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

                Comment

                Working...