How to Remove ‘\x’ From a Hex String in Python?

Problem Formulation + Examples πŸ’¬ Question: Given a string of hexadecimal encoded string such as ‘\x00\xff\xf2’. How to remove the ‘\x’ prefixes or characters from the hex string? Here are a few examples of what you want to accomplish: Hex String Desired Output ‘\x00\xff\xf2′ ’00fff2’ ‘\x41\x42\x43’ ‘414243’ ‘\x53′ ’53’ ‘\xff\xff\xff\xff\xff’ ‘ffffffffff’ ‘\x00\x6a\x6f’ ‘006a6f’ Strawman Solutions … Read more

Python Hex to Float Conversion

Problem Formulation πŸ’¬ Question: Given a hexadecimal string. How to convert it to a float? Examples Here are a few example conversions where each hex string represents four bytes, i.e., two hex characters per byte. Hex String Float ‘0f0f0f0f’ 7.053344520075142e-30 ‘4282cbf1’ 65.39832305908203 ‘43322ffb’ 178.1874237060547 ‘534f11ab’ 889354649600.0 Solution – Convert Hex to Float The expression struct.unpack(‘!f’, … Read more

Python Hex String to Little Endian (Bytes/Integer)

What Is Big/Little Endian? Computers perform computations on data that is represented with bytes, i.e., sequences of 0s and 1s. A computer program manipulates data by loading data from memory (RAM, Cache, Disk, SSD), performing computations based on that data, and storing the resulting data back into the memory. A computer program loads and stores … Read more

Python Hex String to Decimal

Question πŸ’¬ How to convert a hexadecimal string to a decimal integer value in Python? Answer Summary To convert a hex string to a decimal integer value, pass the hex string into the int() function with base argument such as int(‘0xfff’, base=16), or eval() function such as eval(‘0xfff’). The result is a decimal integer value … Read more

How to Filter Data from an Excel File in Python with Pandas

Problem Formulation and Solution Overview This article will show different ways to read and filter an Excel file in Python. To make it more interesting, we have the following scenario: Sven is a Senior Coder at K-Paddles. K-Paddles manufactures Kayak Paddles made of Kevlar for the White Water Rafting Community. Sven has been asked to … Read more

Python Convert Hex String to Binary

πŸ’¬ Question: How to convert a given hex string such as ‘0xF’ to a binary number such as ‘0b1111′? There are multiple variants of this problem depending on how you want the conversion and from which type to which type. In this article, we’ll go over the different methods from simple to more sophisticated. Let’s … Read more

How to Convert Hex String to Bytes in Python?

Problem Formulation Given a string in hexadecimal form: How to convert the hex string to a bytes object in Python? Here are a few examples: Hex String Bytes Object ’01’ b’\x01′ ’04’ b’\x04′ ’08’ b’\x08′ ’01 0a’ b’\x01\n’ ’01 02 0e 0f 0f’ b’\x01\x02\x0e\x0f\x0f’ ‘0f 0f’ b’\x0f\x0f’ Hex String to Bytes using bytes.fromhex(hex_string) To convert … Read more