473,513 Members | 3,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,06,79,187,39,30,59,098,40,25,51,287, 00,05,25,103,44*
to get the checksum.
Thank you for you help.

Phil
Oct 10 '05 #1
7 41321
ord(c) gives you decimal representation of a character.

-Larry Bates

Philipp H. Mohr wrote:
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,06,79,187,39,30,59,098,40,25,51,287, 00,05,25,103,44*
to get the checksum.
Thank you for you help.

Phil

Oct 10 '05 #2
Philipp H. Mohr wrote:
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. ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the byte.
e.g. everything between $ and * needs to be xor:
$GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287, 00,05,25,103,44*
to get the checksum.


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,30,59,098,'
'40,25,51,287,00,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
sc***********@acm.org
Oct 10 '05 #3
On 2005-10-10, Larry Bates <la*********@websafe.com> wrote:
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.
ord(c) gives you decimal representation of a character.


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...
Oct 10 '05 #4
Scott David Daniels <sc***********@acm.org> wrote in
news:43********@nntp0.pdx.net:
Philipp H. Mohr wrote:
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.

ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the
byte.
e.g. everything between $ and * needs to be xor:
$GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287, 00,05,25,
103,44*
to get the checksum.


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,30,59,098,'
'40,25,51,287,00,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
sc***********@acm.org


What is the byte representation of 287?

--
rzed
Oct 10 '05 #5
Scott David Daniels wrote:
Philipp H. Mohr wrote:
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.

ord('a') == 97; chr(97) == 'a'; "ord" gives you the value of the byte.
e.g. everything between $ and * needs to be xor:
$GPGSV,3,1,10,06,79,187,39,30,59,098,40,25,51,287, 00,05,25,103,44*
to get the checksum.


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,30,59,098,'
'40,25,51,287,00,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()


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

Oct 10 '05 #6
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:
On 2005-10-10, Larry Bates <la*********@websafe.com> wrote:

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.


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

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].

Oct 10 '05 #7
[Format recovered from top posting.]

Larry Bates <la*********@websafe.com> writes:
Grant Edwards wrote:
On 2005-10-10, Larry Bates <la*********@websafe.com> wrote:
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.
ord(c) gives you decimal representation of a character.


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].


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).


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.
Probably an "old school" answer on my part.


The decimal representation of ' ' is '32'. Python doesn't think that
that's what ord(' ') returns:
ord(' ') == '32' 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:
ord(' ') == 0x20 True ord(' ') == 040 True ord(' ') == 32

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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 11 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
4109
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
6
10138
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that the data converted from the byte array into a string , is not what i expect. example: - the data returned from the socket is (byte array)...
15
34566
by: Kueishiong Tu | last post by:
How do I convert a Byte array (unsigned char managed) to a char array(unmanaged) with wide character taken into account?
7
66537
by: MilanB | last post by:
Hello How to convert char to int? Thanks
13
2136
by: NISARG | last post by:
Hi, is there any single instruction or operator in 'C' to convert a byte such that MSB will become LSB and so..... for example:11101010 will become 01010111 it can be done by building a function,,but if there is any single instruction than plz tell....
1
19177
by: Elioth | last post by:
Hi... I need to know how to convert Char Array to Byte Array and vice-versa in VB 2K5 Thanks for all help. Elioth
8
5895
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The vFileHandle is supposed to be a file handle (an IntPtr, I suppose). How can I convert this Byte() in this IntPtr ?
3
3819
by: efdeugenio | last post by:
Hi, I will really appreciate if someone cans help me with this: I have a managed c++ class that I am calling from C#. The declaration of a function in this class is: bool CanAddTemplate(unsigned char* template, HRESULT rc, bool bInteractive) When calling this function from c# I have: byte template = new Byte; bresult =...
6
3744
by: ikbel borcheni | last post by:
Hi, how can I convert a char* to a byte array. I tried this unsigned Serveur::CharToByte(char in ) { switch(in){ case'0': case'1': case'2': case'3': case'4': case'5': case'6':case'7': case'8': case'9': return (in-'0'); case'A':case'B': case'C':case'D': case'E':case'F':return(in-'A'+10); } }
0
7394
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7123
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3237
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.