Skip to content Skip to sidebar Skip to footer

Unknown Format Code 'x'

I am working on a program in Python and all the code looks good except when I run the program from the terminal I am getting the following error message: Traceback (most recent cal

Solution 1:

In the following code section, format typex does not expect string. It accepts integer. It then converts integer to its corresponding hexadecimal form.

# Convert the Mac address from the jumbled up form from above into human readable formatdefgetMacAddress(bytesAddress):
    bytesString = map('{:02x}'.format, bytesAddress)
    macAddress = ':'.join(bytesString).upper()

So if your bytestAddress is a string form of integer, then, you could do:

bytesAddress = '123234234'map('{:02x}'.format, [int(i) for i in bytesAddress])
#map('{:02x}'.format, map(int, bytesAddress)) 
['01', '02', '03', '02', '03', '04', '02', '03', '04']

Further, if you want to process a pair of integers (in string form) to hexadecimal, then first transform `bytesAddress to

[bytesAddress[i:i+2] for i in range(0, len(bytesAddress), 2)]

Post a Comment for "Unknown Format Code 'x'"