Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
bytes.hex() Method - Python
Next article icon

bytes.hex() Method - Python

Last Updated : 10 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

bytes.hex() method returns a string representing the hexadecimal encoding of a bytes object. Each byte is represented by two hexadecimal digits making it useful for displaying binary data in a readable form. For Example:

Python
# byte_data d = b'Hello World'  # hex string s = d.hex()  print(s) 

Output
48656c6c6f20576f726c64 

Explanation:

  • bytes.hex() method converts the bytes object b'Hello World' into a hexadecimal string.
  • each byte in the original data is represented by two hexadecimal characters.
  • output "48656c6c6f20576f726c64" is the hex representation of "Hello World".

Table of Content

  • Syntax of bytes.hex()
  • Converting Bytes to Hexadecimal String
  • Displaying Encrypted Data in Hex

Syntax of bytes.hex()

bytes.hex()

Parameters : No parameters.
Return Type: returns a string containing the hexadecimal representation of the bytes object.

Converting Bytes to Hexadecimal String

bytes.hex() method is commonly used to convert binary data into a human-readable hexadecimal format.

Python
# binary_data d = b'Network'  # hex_representation s = d.hex()  print(s) 

Output
4e6574776f726b 

Explanation:

  • bytes.hex() method converts the bytes object b'Network' into a hexadecimal string.
  • each character in "Network" is encoded into its corresponding hexadecimal representation.
  • output "4e6574776f726b" represents "Network" in hexadecimal format.

Displaying Encrypted Data in Hex

This method is useful when displaying encrypted data, allowing it to be easily read or transmitted in a hexadecimal format.

Python
# cipher c = b'SecretKey'  # hex_cipher hc = c.hex()  print(hc) 

Output
5365637265744b6579 

Explanation:

  • bytes.hex() method converts the bytes object b'SecretKey' into a hexadecimal string.
  • each character in "SecretKey" is encoded into its corresponding hexadecimal value.
  • output "5365637265744b6579" represents "SecretKey" in hexadecimal format.

Next Article
bytes.hex() Method - Python

A

aryantcutw
Improve
Article Tags :
  • Python
  • Python-Functions
Practice Tags :
  • python
  • python-functions

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
    MD5 hash in Python
    MD5 is a cryptographic hash function that produces a 128-bit hash value, usually shown as a 32-character hexadecimal string. While it was commonly used for tasks like data integrity checks, MD5 is now considered insecure due to collision vulnerabilities. Despite this, it remains useful for non-sensi
    4 min read
    hex() function in Python
    hex() function in Python is used to convert an integer to its hexadecimal equivalent. It takes an integer as input and returns a string representing the number in hexadecimal format, starting with "0x" to indicate that it's in base-16. Example:Pythona = 255 res = hex(a) print(res)Output0xff Explanat
    2 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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences