473,589 Members | 2,570 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,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
Oct 10 '05 #1
7 41339
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,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

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,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,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,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
sc***********@a cm.org
Oct 10 '05 #3
On 2005-10-10, Larry Bates <la*********@we bsafe.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,0 6,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,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
sc***********@a cm.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,0 6,79,187,39,30, 59,098,40,25,51 ,287,00,05,25,1 03,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,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( )


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*********@we bsafe.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
representati on. 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*********@we bsafe.com> writes:
Grant Edwards wrote:
On 2005-10-10, Larry Bates <la*********@we bsafe.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
representat ion. 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.or g> 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
4121
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
6
10168
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) "Usuario Sin Atribuciones Para Esta Función"
15
34577
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
66622
by: MilanB | last post by:
Hello How to convert char to int? Thanks
13
2147
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
19188
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
5900
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
3841
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 = CIdentificationSet.CanAddTemplate(template, rc, true);
6
3751
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
7931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7990
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5731
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3849
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3889
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2374
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
0
1198
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.