468,720 Members | 1,597 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

Conversion from string to integer

Hi,

I've been attempting to write a serial program in python. I talk to
a custom designed bit of hardware that sends me back groups that are 2
bytes in length. When i recieve them using either pySerial or USPP i'm
not sure how python interprets them. Both of the bytes should be
interpreted as one integer. I store each of the two bytes in a python
array. Here is an example of the data as printed in a terminal.

['\x1dz', '\xa8<', '\x89{', '}O', 'r\xaf', '\x83\xcd', '\x81\xba',
'\x00\x02', '\x00\x00', '\x00\x00', '\x00\x00']

As you can see it either chooses to represent one byte in hex or one in
ascii or both in hex etc. I'm just not sure how to get this into an
integer format.

Thanks for the help.

Regards,
Ken

Mar 22 '06 #1
6 2267
xkenneth wrote:
I've been attempting to write a serial program in python. I talk to
a custom designed bit of hardware that sends me back groups that are 2
bytes in length. When i recieve them using either pySerial or USPP i'm
not sure how python interprets them. Both of the bytes should be
interpreted as one integer. I store each of the two bytes in a python
array. Here is an example of the data as printed in a terminal.

['\x1dz', '\xa8<', '\x89{', '}O', 'r\xaf', '\x83\xcd', '\x81\xba',
'\x00\x02', '\x00\x00', '\x00\x00', '\x00\x00']

As you can see it either chooses to represent one byte in hex or one in
ascii or both in hex etc. I'm just not sure how to get this into an
integer format.


Your data consists entirely of two-byte strings; don't get fooled by the way
Python represents them. To convert them you need struct.unpack() which
seems to be in high demand today:
data = ['\x1dz', '\xa8<', '\x89{', '}O', 'r\xaf', '\x83\xcd', '\x81\xba',
.... '\x00\x02', '\x00\x00', '\x00\x00', '\x00\x00'] print [struct.unpack("h", s)[0] for s in data]

[7546, -22468, -30341, 32079, 29359, -31795, -32326, 2, 0, 0, 0]

Of course "h" is only one of the candidates for the format parameter. See
http://docs.python.org/lib/module-struct.html for the complete list.

Peter

Mar 22 '06 #2
You want something like this:
a = '\x1dz'
(ord(a[0])<<8) + ord(a[1])

7546

Each of the two characters represents one byte of a 16-bit integer. It
doesn't matter if they are ascii or hex -- there are still exactly two
bytes in each of your strings.

The ord() function converts a character to its 8-bit ASCII code. The
<< operator shifts the first character 8 bits to the left. Note that
the byte order might need to be swapped, depending on the
implementation of your custom hardware. e.g. (ord(a[1])<<8) +
ord(a[0])

Mar 22 '06 #3

"xkenneth" <xk******@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Hi,

I've been attempting to write a serial program in python. I talk to
a custom designed bit of hardware that sends me back groups that are 2
bytes in length. When i recieve them using either pySerial or USPP i'm
not sure how python interprets them. Both of the bytes should be
interpreted as one integer. I store each of the two bytes in a python
array. Here is an example of the data as printed in a terminal.

['\x1dz', '\xa8<', '\x89{', '}O', 'r\xaf', '\x83\xcd', '\x81\xba',
'\x00\x02', '\x00\x00', '\x00\x00', '\x00\x00']

As you can see it either chooses to represent one byte in hex or one in
ascii or both in hex etc. I'm just not sure how to get this into an
integer format.


Look at array and/or struct modules/

Mar 22 '06 #4
Peter Otten wrote:
To convert them you need struct.unpack()


Ahh, right. Batteries included! Thanks for the enlightenment. :)

Mar 22 '06 #5
On 2006-03-22, Mark Warburton <Ma************@gmail.com> wrote:
Peter Otten wrote:
To convert them you need struct.unpack()


Ahh, right. Batteries included! Thanks for the enlightenment. :)


I think that's the third time _today_ that question has been
answered. Where can we put that information such that you
would have found it?

--
Grant Edwards grante Yow! World War Three can
at be averted by adherence
visi.com to a strictly enforced
dress code!
Mar 22 '06 #6
Grant Edwards wrote:
On 2006-03-22, Mark Warburton <Ma************@gmail.com> wrote:
Ahh, right. Batteries included! Thanks for the enlightenment. :)


I think that's the third time _today_ that question has been
answered. Where can we put that information such that you
would have found it?


Don't ask me -- I didn't ask the original question! I just answered it
in a different way.

Mar 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ken | last post: by
4 posts views Thread by Jim Hubbard | last post: by
2 posts views Thread by Thomas Matthews | last post: by
1 post views Thread by heirou | last post: by
7 posts views Thread by Alphonse Giambrone | last post: by
15 posts views Thread by Peter Afonin | last post: by
1 post views Thread by =?Utf-8?B?TWlrZSBN?= | last post: by
1 post views Thread by Oskars | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.