bytes.fromhex() Method - Python Last Updated : 10 Mar, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report bytes.fromhex() method converts a hexadecimal string into a bytes object. Each pair of hex digits represents one byte making it useful for decoding hexadecimal-encoded data. For Example: Python hex_str = "48656c6c6f20576f726c64" # Hex representation of "Hello World" byte_data = bytes.fromhex(hex_str) print(byte_data) Outputb'Hello World' Syntax of bytes.fromhex()bytes.fromhex(string)Parameters: string: A hexadecimal string containing only valid hex characters (0-9, A-F).Return: Returns a bytes object containing the decoded binary data.Converting Hexadecimal Strings to BytesWhen working with hexadecimal-encoded data, bytes.fromhex() helps in decoding it back into a bytes object. Python hex_code = "4e6574776f726b" res = bytes.fromhex(hex_code) print(res) Outputb'Network' Explanation:hexadecimal string "4e6574776f726b" corresponds to the word "Network".bytes.fromhex(hex_code) decodes the hex string into the bytes object: b'Network'.Processing Encrypted DataThis method is often used in cryptography or network communication when dealing with encoded binary data. Python # cipher_text s = "5365637265744b6579" # decoded_cipher o = bytes.fromhex(s) print(o) Outputb'SecretKey' Explanation:hexadecimal string "5365637265744b6579" represents the ASCII encoding of "SecretKey".bytes.fromhex(s) converts this hex string into the bytes object: b'SecretKey'.output of print(o) is b'SecretKey', showing the decoded string as bytes. Comment More infoAdvertise with us Next Article Python | os.fsdecode() method A aryantcutw Follow Improve Article Tags : Python Practice Tags : python Similar Reads Python bytes() method bytes() method in Python is used to create a sequence of bytes. In this article, we will check How bytes() methods works in Python. Pythona = "geeks" # UTF-8 encoding is used b = bytes(a, 'utf-8') print(b)Outputb'geeks' Table of Contentbytes() Method SyntaxUsing Custom EncodingConvert String to Byte 3 min read Python | os.fsdecode() method OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.fsdecode() method in Python is used to decode the specified filename from the 1 min read Python Strings decode() method The decode() method in Python is used to convert encoded text back into its original string format. It works as the opposite of encode() method, which converts a string into a specific encoding format. For example, let's try to encode and decode a simple text message:Pythons = "Geeks for Geeks" e = 4 min read bytearray() function - Python The bytearray() function in Python creates a mutable sequence of bytes, which is essentially an array of integers in the range 0 to 255 (representing byte values). Unlike the immutable bytes type, bytearray allows us to modify its contents after creation, making it useful for tasks like binary data 5 min read Python - Write Bytes to File Files are used in order to store data permanently. File handling is performing various operations (read, write, delete, update, etc.) on these files. In Python, file handling process takes place in the following steps:Open filePerform operationClose fileThere are four basic modes in which a file can 3 min read Convert Hex String to Bytes in Python Converting a hexadecimal string to bytes in Python involves interpreting each pair of hexadecimal characters as a byte. For example, the hex string 0xABCD would be represented as two bytes: 0xAB and 0xCD. Letâs explore a few techniques to convert a hex string to bytes.Using bytes.fromhex() bytes.fro 2 min read Like