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"
#8
(Oct-09-2019, 05:59 PM)Gribouillis Wrote: Could it be an error similar to this one ?
>>> rom_bytes = b'hello world'
>>> import struct
>>> struct.unpack("I", rom_bytes[4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'int'
>>> struct.unpack("I", rom_bytes[:4])
(1819043176,)

I don't understand why would be, since is a different error message.

Anyway, I don't know why, but now the code is working on python 3.2, since it converts the values to int. However in 2.6 where the type is 'str', first I receive
Quote:self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE *(self.num_prg_blocks))]
TypeError: unsupported operand type(s) for +: 'int' and 'str'


Then adding a int before with:

self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE * int (self.num_prg_blocks))]
I receive:

Quote:ValueError: invalid literal for int() with base 10: '\x02'

finally doing this:

self.temp = self.num_prg_blocks
		struct.unpack ("I", self.temp)
        self.data_bytes = rom_bytes [HEADER_SIZE:HEADER_SIZE + (16 + KB_SIZE * self.temp)] 
makes me receive on python 2.6

Quote:struct.error: unpack requires a string argument of length 4

and on python 3.2

Quote:TypeError: 'int' does not support the buffer interface


What shall be my next step?
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