Python Forum
Problem with "invalid literal for int() with base 10: '' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with "invalid literal for int() with base 10: '' (/thread-7752.html)



Problem with "invalid literal for int() with base 10: '' - jirkaj4 - Jan-23-2018

Hello friends.

I try calculate CRC32 from my binary file. But Anythink is wrong.

Here is function to read and print block of bin file. It is working very fine.
def ReadFile(offset, blok):
	i = -15
	filename = "EMPTY.bin"
	blocksize = blok
	
	opts,args = getopt.getopt(sys.argv[1:],'f:b:')
	
	for o,a in opts:
		if o == '-f':
			filename = a
		if o == '-b':
			blocksize = a
	
	with open(filename,"rb") as f:
		f.seek(offset, 1)
		block = f.read(blocksize)
		str = ""
		odsaz = ""

		for ch in block:
			str += hex(ord(ch))[2:].zfill(2)+" "
			if (i % 16 == 0):
				odsaz += hex(i)[2:].zfill(8) + ": " + str + "\n"
				str = ""
			i = i + 1
		#vypise do konzole vygenerovany log
		print odsaz
		return block
And I have finction to calculate CRC32:
custom_crc_table = {}

def int_to_bytes(i):
    return [(i >> 24) & 0xFF, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF]


def generate_crc32_table(_poly):

    global custom_crc_table

    for i in range(256):
        c = i << 24

        for j in range(8):
            c = (c << 1) ^ _poly if (c & 0x80000000) else c << 1

        custom_crc_table[i] = c & 0xffffffff


def custom_crc32(buf):

    global custom_crc_table
    crc = 0xffffffff

    for integer in buf:
        b = int_to_bytes(integer)

        for byte in b:
            crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[(crc >> 24) ^ byte]

    return crc
And problem is this. I have this main:
crc.generate_crc32_table(poly)
data = ReadFile(0, 1024)
custom_crc = crc.custom_crc32(int(data))
And I get error:
  • File "C:\Users\jirka\Desktop\CRC32\test.py", line 63, in <module>
    custom_crc = crc.custom_crc32(int(data))
    ValueError: invalid literal for int() with base 10: ''

Can you help me, how to solve the problem? Thank you.


RE: Problem with "invalid literal for int() with base 10: '' - Windspar - Jan-23-2018

data = '0xffffffff'
int(data, 16)



RE: Problem with "invalid literal for int() with base 10: '' - wavic - Jan-23-2018

Print 'data' before to convert it to integer and use it to calculate the crc and you will see why is that.


RE: Problem with "invalid literal for int() with base 10: '' - Windspar - Jan-23-2018

or
data = 0xffffffff
int(hex(data), 16)



RE: Problem with "invalid literal for int() with base 10: '' - jirkaj4 - Jan-23-2018

Are you thinging this?
data = 0xffffffff
	data = ReadFile(0, 1024)

	custom_crc = crc.custom_crc32(int(hex(data), 16))
Because, now I get new error:
  • custom_crc = crc.custom_crc32(int(hex(data), 16))
    TypeError: hex() argument can't be converted to hex