473,222 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,222 software developers and data experts.

String to Int

I'm developing some Python code for an embedded GPRS module.
The inside Python interpeter version is 1.5.2+
I had no debugging opportunities on the module

The following code gives some problem and it has to do something with string conversion.

Expand|Select|Wrap|Line Numbers
  1. def calculatecrc( message="" ):
  2.     remainder = 0
  3.     data = 0
  4.     for i in range(0,len(message)):
  5.         data = message[i:i+1] ^ (remainder)
  6.         remainder = arr[data] ^ (remainder << 8)
  7.     SER.send(str(remainder) + '\r\n')
  8.  
The line "data = message[i:i+1] ^ (remainder)" gives the problems.
How can I solve this problem in Python 1.5.2+ interpeter?
Sep 24 '07 #1
5 5667
bvdet
2,851 Expert Mod 2GB
I'm developing some Python code for an embedded GPRS module.
The inside Python interpeter version is 1.5.2+
I had no debugging opportunities on the module

The following code gives some problem and it has to do something with string conversion.

Expand|Select|Wrap|Line Numbers
  1. def calculatecrc( message="" ):
  2.     remainder = 0
  3.     data = 0
  4.     for i in range(0,len(message)):
  5.         data = message[i:i+1] ^ (remainder)
  6.         remainder = arr[data] ^ (remainder << 8)
  7.     SER.send(str(remainder) + '\r\n')
  8.  
The line "data = message[i:i+1] ^ (remainder)" gives the problems.
How can I solve this problem in Python 1.5.2+ interpeter?
The bitwise xor operator '^' can only be applied to integers and long integers. It appears that variable 'message' is a string and you are applying a slice of it to '^'.
Sep 24 '07 #2
bartonc
6,596 Expert 4TB
I'm developing some Python code for an embedded GPRS module.
The inside Python interpeter version is 1.5.2+
I had no debugging opportunities on the module

The following code gives some problem and it has to do something with string conversion.

Expand|Select|Wrap|Line Numbers
  1. def calculatecrc( message="" ):
  2.     remainder = 0
  3.     data = 0
  4.     for i in range(0,len(message)):
  5.         data = message[i:i+1] ^ (remainder)
  6.         remainder = arr[data] ^ (remainder << 8)
  7.     SER.send(str(remainder) + '\r\n')
  8.  
The line "data = message[i:i+1] ^ (remainder)" gives the problems.
How can I solve this problem in Python 1.5.2+ interpeter?
My friend, bvdet, is correct: The slice syntax used here looks very odd, indeed. But in this particular case, it would work because [i:i+1] has a length of one. This would be a lot cleaner:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     for c in message:
  3.         data = ord(c) ^ remainder
Sep 24 '07 #3
My friend, bvdet, is correct: The slice syntax used here looks very odd, indeed. But in this particular case, it would work because [i:i+1] has a length of one. This would be a lot cleaner:
Expand|Select|Wrap|Line Numbers
  1. #
  2.     for c in message:
  3.         data = ord(c) ^ remainder

It looks a lot cleaner, but it didn't work
ord() makes an integer from every character in the string, so I realy don't understand what is going wrong.
Sep 27 '07 #4
bartonc
6,596 Expert 4TB
It looks a lot cleaner, but it didn't work
ord() makes an integer from every character in the string, so I realy don't understand what is going wrong.
The syntax that I gave is equivalent to the one that you used:
Expand|Select|Wrap|Line Numbers
  1. >>> message = 'hello world'
  2. >>> 
  3. >>> message = 'hello world'
  4. >>> for i in range(0,len(message)):
  5. ...     print message[i:i+1],
  6. ...     
  7. h e l l o   w o r l d
  8. >>> for c in message:
  9. ...     print c,
  10. ...     
  11.  h e l l o   w o r l d
  12. >>> 
So we may need to work on your CRC logic a bit. I'll have a look.
And, perhaps, you could supply some more details of your implementation.
Sep 27 '07 #5
bartonc
6,596 Expert 4TB
The syntax that I gave is equivalent to the one that you used:
Expand|Select|Wrap|Line Numbers
  1. >>> message = 'hello world'
  2. >>> 
  3. >>> message = 'hello world'
  4. >>> for i in range(0,len(message)):
  5. ...     print message[i:i+1],
  6. ...     
  7. h e l l o   w o r l d
  8. >>> for c in message:
  9. ...     print c,
  10. ...     
  11.  h e l l o   w o r l d
  12. >>> 
So we may need to work on your CRC logic a bit. I'll have a look.
And, perhaps, you could supply some more details of your implementation.
Unless, of course, you are doing this bit-wise, and your message looks like "010100110101". I that case you'd use
Expand|Select|Wrap|Line Numbers
  1. int(c)
Sep 27 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
2
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
11
by: ramu | last post by:
Hi, Suppose I have a string like this: "I have a string \"and a inner string\\\" I want to remove space in this string but not in the inner string" In the above string I have to remove...
8
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.