"Int() Can't Convert Non-String with Explicit Base" but It Is a String

I'm trying to convert some hex input into decimal RGB. Apologies if the code is bad, I'm new at python but I'm getting the error in the title even tho the input is a string? (I already printed the stuff to be converted into decimal before converting and they are all string)

v1 = input("Enter the first pixel value: ")
v2 = input("Enter the second pixel value: ")
#px_to_gen = input ("Enter the number of pixels to generate in between: ")

#group (r g b) values of v1 & v2 in a list
v1_ls = ["".join((v1[2],v1[3])),"".join((v1[4],v1[5])),"".join((v1[6],v1[7]))]
v2_ls = ["".join((v2[2],v2[3])),"".join((v2[4],v2[5])),"".join((v2[6],v2[7]))]

print(v1_ls)
print(v2_ls)

#convert values in v1_ls & v2_ls to decimal
i = 0
while (i <= 2):
    v1_ls[i] = int(v1_ls[i], 16)
    v2_ls[i] = int(v2_ls[i], 16)

print(v1_ls)
print(v2_ls)
7

1 Answer

I assume you're facing this error: TypeError: int() can't convert non-string with explicit base

This is because you're trying to convert a list directly to integer with base conversion.

To fix this you can try below code, (converting into string first).

v1_ls[i] = int(str(v1_ls[i]), 16)
v2_ls[i] = int(str(v2_ls[i]), 16)

But please remember that you need to fix a big issue with your while loop - it'll go in a infinite loop.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest