Python Forum
How to convert "str" to "int"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert "str" to "int"
#10
(Oct-12-2019, 10:39 PM)Gribouillis Wrote:
colt Wrote:I don't know why, but now the code is working on python 3.2, since it converts the values to int.
That's because in python 3 a bytes string is a sequence of small integers:
>>> # python 3
>>> x = b'\x02'
>>> x[0]
2
in python 2
>>> # python 2
>>> x = '\x02'
>>> x[0]
'\x02'
In python 2 you can convert a one character string to int by using the ord function
>>> ord(x[0])
2
But when calling struct.unpack ("I", self.temp), make sure self.temp is a 4 bytes string.

Finally, do you have to use python 2? Don't use it, it's dead.

Tried
self.num_prg_blocks = ord (self.header [4])
and it solved this issue with old python, but with the new python I receive:
Quote:TypeError: ord() expected string of length 1, but int found

To answer, I do not HAVE to use it, as mandatory
Reply


Messages In This Thread
How to convert "str" to "int" - by colt - Oct-06-2019, 01:57 AM
RE: How to convert "str" to "int" - by scidam - Oct-06-2019, 05:25 AM
RE: How to convert "str" to "int" - by Gribouillis - Oct-06-2019, 06:38 AM
RE: How to convert "str" to "int" - by colt - Oct-08-2019, 11:13 PM
RE: How to convert "str" to "int" - by Gribouillis - Oct-09-2019, 05:51 AM
RE: How to convert "str" to "int" - by colt - Oct-09-2019, 05:40 PM
RE: How to convert "str" to "int" - by Gribouillis - Oct-09-2019, 05:59 PM
RE: How to convert "str" to "int" - by colt - Oct-12-2019, 09:55 PM
RE: How to convert "str" to "int" - by Gribouillis - Oct-12-2019, 10:39 PM
RE: How to convert "str" to "int" - by colt - Oct-15-2019, 03:17 PM
RE: How to convert "str" to "int" - by Gribouillis - Oct-15-2019, 03:45 PM
RE: How to convert "str" to "int" - by colt - Oct-15-2019, 11:40 PM
RE: How to convert "str" to "int" - by Gribouillis - Oct-16-2019, 06:14 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020