How to Convert an Octal Escape Sequence in Python – And Vice Versa?

This tutorial will show you how to convert an But let’s quickly recap what an octal escape sequence is in the first place! 👇 What Is An Octal Escape Sequence?💡 An Octal Escape Sequence is a backslash followed by 1-3 octal digits (0-7) such as \150 which encodes the ASCII character ‘h’. Each octal escape … Read more

Python Binary String to ASCII String – and Vice Versa

Problem Formulation In this article, you’ll learn how to convert binary string (values) to an ASCII string in Python. For example, you may want to convert the binary string: 0110100001100101011011000110110001101111001000000111011101101111011100100110110001100100 to the ASCII text: hello world Using a simple Python script! đŸ’Ŧ Question: How would we write Python code to perform the binary to ASCII … Read more

Python Convert Hex to Base64

đŸ’Ŧ Question: How to convert a hexadecimal string such as 02af01ff00 to a normal string using the Base64 format in Python? 👉 Short answer: Use the following fancy one-liner expression to convert the hex string s to a Base64-encoded Python string: base64.b64encode(bytes.fromhex(s)).decode(). For the long answer, keep reading! đŸĨ¸ If you’re like me, you may … Read more

How to Write a Hex String as Binary Data & Binary File in Python?

Hex String as Binary Data To convert a hex string such as ‘FF0001AF’ to binary data, use the binascii.unhexlify(hex_str) function that returns the binary data represented by the hexadecimal string. Note that this converts two hex string digits to one byte, i.e., hex string ‘F0’ is converted to the byte representation 11110000, or in binary … Read more

Format Integer List to Hex String – Six Pythonic Ways

Problem Formulation đŸ’Ŧ Question: How to create a string of hex digits from a list of integers (0–255) so that each hex digit consists of two digits such as “00”, “01”, …, “fe”, “ff”? Here’s an example input/output pair: In: [0, 1, 2, 3, 255, 254, 253] Out: ‘00010203fffefd’ Method 1: Bytearray The easiest way … Read more