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"
#4
(Oct-06-2019, 05:25 AM)scidam Wrote: Take a look at the struct module.

import struct
four_bytes = b'abcd'  # sample bytesting
struct.unpack("I", four_bytes) # interpret bytesting as unsigned int

From what I see the difference is that you used "I" instead my "h". I changed it and didn't change the error message.

(Oct-06-2019, 06:38 AM)Gribouillis Wrote: Are you sure it's this line that causes this error message? If 'header' is a bytes string (which you can check by printing the type of the variable) there should be no problem accessing header[4], and it must be an integer. In the worst case it could raise an IndexError.

It says line 11, which is the one that uses struct.unpack. I checked the type and it shows
Quote:<class 'bytes'>
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