Python Forum

Full Version: Conversion from hexa to decimal in loop bugging
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys,

I believe I'm hitting a bug, here is my piece of code:

def allcolors():

    query = db.session.query(BLColor).order_by(BLColor.color_name)
    colors = query.all()

    for c in colors:
        #print(type(c.color_rgb))
        background = str(c.color_rgb) #just making sure it is a string c.color_rgb example is FE55AE
        split_strings = re.findall('..',background) #splitting XXYYZZ into a list of [XX, YY, ZZ] => split_strings = ['68', 'BC', 'C5']
        print(split_strings) #['68', 'BC', 'C5']
        d1 = split_strings[0] # get the first item of the list, to convert it to decimal ex: d1 = '68'
        print(d1)

    decim = int(d1, 16) #convert from hexa to decimal
    print('Value in hexadecimal:', d1)
    print('Value in decimal:', decim)

    return render_template('all-colors.html', parts=colors)
This code is working 100% fine, it does convert the last d1 value generated in the c in colors loop to a decimal

The problem is that I want to convert all background color RGB value to decimal.
When I move this block in the loop
    decim = int(d1, 16)
    print('Value in hexadecimal:', d1)
    print('Value in decimal:', decim)
If should definitely work, but I get this error:
Error:
Traceback (most recent call last): File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 2091, in __call__ return self.wsgi_app(environ, start_response) File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 2076, in wsgi_app response = self.handle_exception(e) File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "D:\Scripts\python\project55\venv\Lib\site-packages\flask\app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "D:\Scripts\python\project55\lego2\routes.py", line 294, in allcolors decim = int(d1, 16) ValueError: invalid literal for int() with base 16: 'ne'
Well, "ne" is not a hexadecimal number.
Error:
decim = int(d1, 16) ValueError: invalid literal for int() with base 16: 'ne'
I can duplicate the error:
import re

def allcolors():
    colors = ("neAABB",)
    for c in colors:
        background = c
        split_strings = re.findall('..' ,background)
        print(split_strings)
        d1 = split_strings[0] # get the first item of the list, to convert it to decimal
        print(d1)

        decim = int(d1, 16)
        print('Value in hexadecimal:', d1)
        print('Value in decimal:', decim)

allcolors()
Error:
decim = int(d1, 16) ValueError: invalid literal for int() with base 16: 'ne'
Now you just need to figure where 'ne' is coming from. My guess is there is something in BLColor that is not a color. The code stopped working you started processing all the colors instead of just the last one.