PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How do we use Python code to convert a list of integers in decimal format to hexadecimal using a block of code?

 August 10, 2022     decimal, hex, python     No comments   

Issue

Here is the background information: I have a .txt file with some numbers on it that are all separated by commas. Then, using Python code, I opened it and read it. After that, I made a list out of all of the numbers that were on the file. When I made a list, the numbers were all strings (example: '9', '8', etc.), so I used some Python code to convert the values of the list into integers. Now, I am trying to create some code so that I can convert all of the integers in the list to hexadecimal. The reason I want to create a piece of code instead of doing it manually is that there are around 20 numbers (in decimal format) and I want to convert them all into hexadecimal using some piece of Python code. Here is my code so far (until the point of converting the list to integers). Please help! I have tried to search so much on Google but could not find anything specific to this.

Code:

my_file = open(r'C:\Users\USER\Downloads\List of Numbers File.txt', encoding='utf-8-sig')

content = my_file.read()

print(content)

content_list = content.split(",")

my_file.close()

print(content_list)

for i in range(0, len(content_list)):
    content_list[i] = int(content_list[i])

print(str(content_list))

Solution

Can be done in one line by

hex_list = [hex(int(x)) for x in content_list]


Answered By - Divyanshu Srivastava
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing