bytes.fromhex() Method - Python Last Updated : 10 Mar, 2025 Comments Improve Suggest changes 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 bytes() 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 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 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 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 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 Like