Python Hex String to Integer List - GeeksforGeeks (2024)

Last Updated : 12 Mar, 2024

Summarize

Comments

Improve

Hexadecimal strings are commonly encountered when working with low-level programming, networking, or cryptographic applications. In Python, converting a hex string to an integer array or list is a frequent task. In this article, we’ll explore some different methods to achieve this conversion.

Hex String To Integer Array

We change a Hex string into a list of numbers, where every 2 hex values make one number. Look at the picture below to see how this change happens. We group 4 bits from each hex to create an 8-bit number. These numbers can range from 0 to 255.
Python Hex String to Integer List - GeeksforGeeks (1)

Python Hex String To Integer Array Or List

Below, are the ways to convert Python Hex String To Integer Array Or List.

Python Hex String To Integer Array Or List Using map()

In this example, below code transforms the hex string “1a2b3c4d” into an integer array using a lambda function and map(). It prints the original hex string and the resulting integer array, but there’s a typo in the print statement; it should be corrected to “integerArray.”

Python3

hexString = "1a2b3c4d"

intArray = list(map(lambda x: int(x, 16),

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

print("Hex String: ",hexString)

print("Integer array: ",intArray)

Output

Hex String: 1a2b3c4dInteger array: [26, 43, 60, 77]

Python Hex String To Integer Array Or List Using List Comprehension

In this example, below code converts the hex string ’48E59C7′ into an integer array, where each pair of hex values corresponds to an integer. It then prints both the original hex string and the resulting integer array.

Python3

hexString = '48E59C7'

intArray = [int(hexString[i:i+2], 16) for i in range(0, len(hexString), 2)]

print("Hex String: ",hexString)

print("Integer array: ",intArray)

Output

Hex String: 48E59C7Integer array: [72, 229, 156, 7]

Python Hex String To Integer Array Or List Using bytes.from( )

In this example, below code transforms the hex string ’48E59C’ into an integer array using the `bytes.fromhex()` method. The resulting integer array is then printed, along with the original hex string.

Python3

hexString = '48E59C'

intArray = list(bytes.fromhex(hexString))

print("Hex String: ",hexString)

print("Integer array: ",intArray)

Output

Hex String: 48E59CInteger array: [72, 229, 156]

Conclusion

In conclusion, converting a hex string to an integer array or list in Python can be accomplished through various methods. Whether using list comprehension, map(), bytearray.fromhex(), or struct.unpack(), each approach provides a distinct way to handle the conversion. The choice of method may depend on factors such as readability, performance, or specific project requirements.



Please Login to comment...

Python Hex String to Integer List - GeeksforGeeks (2024)
Top Articles
What Will Happen If My SSL Certificate Expires?
Which years are leap years and can you have leap seconds?
Where To Go After Howling Pit Code Vein
Skyward Sinton
Craglist Oc
Caroline Cps.powerschool.com
Hotels Near 500 W Sunshine St Springfield Mo 65807
Flights to Miami (MIA)
Mawal Gameroom Download
Evita Role Wsj Crossword Clue
Joe Gorga Zodiac Sign
Www.paystubportal.com/7-11 Login
Keurig Refillable Pods Walmart
Hope Swinimer Net Worth
About Us | TQL Careers
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
2015 Honda Fit EX-L for sale - Seattle, WA - craigslist
Best Suv In 2010
Unlv Mid Semester Classes
24 Hour Drive Thru Car Wash Near Me
2020 Military Pay Charts – Officer & Enlisted Pay Scales (3.1% Raise)
Royal Cuts Kentlands
Teacup Yorkie For Sale Up To $400 In South Carolina
Lisas Stamp Studio
Understanding Gestalt Principles: Definition and Examples
Drift Hunters - Play Unblocked Game Online
Barista Breast Expansion
Villano Antillano Desnuda
Ts Modesto
Does Royal Honey Work For Erectile Dysfunction - SCOBES-AR
Ff14 Sage Stat Priority
Shauna's Art Studio Laurel Mississippi
Magicseaweed Capitola
10 games with New Game Plus modes so good you simply have to play them twice
Labyrinth enchantment | PoE Wiki
„Wir sind gut positioniert“
Google Flights Orlando
Exploring the Digital Marketplace: A Guide to Craigslist Miami
Craigslist Minneapolis Com
Sound Of Freedom Showtimes Near Amc Mountainside 10
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
Unblocked Games - Gun Mayhem
Sherwin Source Intranet
Iron Drop Cafe
300+ Unique Hair Salon Names 2024
Rocket Bot Royale Unblocked Games 66
Minecraft Enchantment Calculator - calculattor.com
The Missile Is Eepy Origin
Ff14 Palebloom Kudzu Cloth
211475039
The Love Life Of Kelsey Asbille: A Comprehensive Guide To Her Relationships
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5478

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.