471,344 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 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 5462
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

Post your reply

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

Similar topics

16 posts views Thread by Krakatioison | last post: by
5 posts views Thread by Stu Cazzo | last post: by
9 posts views Thread by John F Dutcher | last post: by
10 posts views Thread by Angus Leeming | last post: by
2 posts views Thread by Andrew | last post: by
29 posts views Thread by zoro | last post: by
2 posts views Thread by Badass Scotsman | last post: by
15 posts views Thread by morleyc | last post: by

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.