Python Forum
Conversion from hexa to decimal in loop bugging
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conversion from hexa to decimal in loop bugging
#1
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'
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Write the XML file from elementtree with hexa decimal encoding Dillibabu 4 3,489 Dec-24-2019, 10:10 AM
Last Post: Dillibabu
  Change of mass hexa data kosteloos 0 1,779 Aug-12-2019, 10:04 AM
Last Post: kosteloos
  testing for Decimal w/o importing decimal every time Skaperen 7 4,472 May-06-2019, 10:23 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020