473,796 Members | 2,916 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2464
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.goo glegroups.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11163
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File "<pyshell#2>", line 1, in ? y=long(x)
4
2772
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it here..... -------- // // a set of simple C# wrappers over the NT Defragmenter APIs //
2
3095
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I want to create a function, preferably a conversion operator, to convert from a std::string to an AnsiString. Please note that I don't want to change either std::string or AnsiString.
1
11689
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in Korea. I use two fields: a date field, and a time field. I need the converted time to show up in a report. I also need the corresponding date to increment if necessary. Any ideas on how to do this would be greatly appreciated.
7
1704
by: Alphonse Giambrone | last post by:
How can I convert a string to a different type based on another string or other variable? For instance, instead of Dim i as Integer i = ctype("1000", Integer) I would like to do
15
2441
by: Peter Afonin | last post by:
Hello, I'm struggling with the string conversion to MD5 which I've never user before. I have a string that I need to encode which looks approximately like this: "pva:0.05:101214:pa7735tH:inv_desc=205308:shp_Email=petera_gudzon.net:lang =ru:shp_PaymentNo=20040825205308:shp_UserID=pva:shp_Price=2.95:shp_HostPlan= BU:shp_Term=2"
5
5782
by: Ian Rutherford | last post by:
Heya guys, It seems VB .net no longer supports the awesome ability of VB 6 to declare something as a string and specify how long the string would be all in one line: Public myString as String * 32 'this would make it 32 characters long Apparently the VB .net conversion is to do this: Public myString as String myString = new String(CChar(" "), 32) 'this creates a string with 32 blank spots in it
12
8847
by: Dennis | last post by:
I get the following results: Cint(&HC5798A2F) returns -981890513 as it should and Clng("&HFFFFFFFFC5798A2F") returns -981890513 as it should. However when using a string Cint("(&HC5798A2F") returns an overflow exception.
0
2223
by: Jm | last post by:
Hi All I recently got some help with code conversion that i used in vb6 to block a specific tcp port. The code runs under a timer and is run on a very low tick count (Meaning it happens constantly). Unfortunately the code appears to do nothing ? This is the converted code Public Structure MIB_TCPROW Public dwState As Integer Public dwLocalAddr As Integer
1
1792
by: =?Utf-8?B?TWlrZSBN?= | last post by:
Hi, My conversion seems from VB6 to VB.Net of an interface to an API seem to have gone OK, but when I look at the output from the Debug.Print("Active server is " & xmlServerActive) statement, xmlServerActive only contains one character(the first one). I suspect it is being truncated. I am pretty much a novice at this sort of thing so any advice would be welcome. Here is the old VB6 code: Listening for Servers and System Status using VB
0
9685
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
10467
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
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
10021
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
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
6802
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
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...
1
4130
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
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.