Skip to content

Commit 05b37c7

Browse files
[Term Entry] Python binascii-module: .hexlify() (#7894)
* [Edit] Python: Python CLI arguments * Update command-line-arguments.md * [Term Entry] Python binascii-module: .hexlify() * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt * Apply suggestion from @avdhoottt ---------
1 parent 09faf3b commit 05b37c7

File tree

1 file changed

+74
-0
lines changed
  • content/python/concepts/binascii-module/terms/hexlify

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---
2+
Title: '.hexlify()'
3+
Description: 'Encodes binary data into a hexadecimal (base-16) representation.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
- 'Data Science'
8+
Tags:
9+
- 'Encoding'
10+
- 'Methods'
11+
- 'Modules'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/computer-science'
15+
---
16+
17+
The **`.hexlify()`** method from Python’s [`binascii`](https://www.codecademy.com/resources/docs/python/binascii-module) module encodes binary data into a hexadecimal (base-16) ASCII representation. It’s an alias for `binascii.b2a_hex()` and is commonly used to represent binary data in a readable hex format. Each byte of input data is converted into a two-digit hexadecimal value.
18+
19+
## Syntax
20+
21+
```pseudo
22+
binascii.hexlify(data[, sep[, bytes_per_sep]])
23+
```
24+
25+
**Parameters:**
26+
27+
- `data` (bytes-like): The binary data to encode.
28+
- `sep` (optional, single character `str` or `bytes`): A separator to insert between hex groups.
29+
- `bytes_per_sep` (optional `int`): After every `bytes_per_sep` input bytes, insert `sep`. A negative value will count from the right end.
30+
31+
**Return value:**
32+
33+
Returns a `bytes` object containing the hexadecimal ASCII representation of the input.
34+
35+
> **Note:** The returned object is always twice as long as the original data. If `sep` is provided, it’s inserted at the specified intervals for better readability.
36+
37+
## Example: Converting Binary Data to Hexadecimal
38+
39+
This example encodes a byte string into its hexadecimal equivalent:
40+
41+
```py
42+
import binascii
43+
44+
message = b'Python3'
45+
hex_value = binascii.hexlify(message)
46+
print(hex_value)
47+
```
48+
49+
The output of this code is:
50+
51+
```shell
52+
b'507974686f6e33'
53+
```
54+
55+
This converts each byte of `'Python3'` into a two-character hexadecimal value, producing a readable byte representation.
56+
57+
## Codebyte Example: Converting to Hexadecimal and Decoding to String
58+
59+
This codebyte demonstrates converting binary data into its hexadecimal representation using `.hexlify()`:
60+
61+
```codebyte/python
62+
import binascii
63+
64+
# Original binary data
65+
data = b'OpenAI'
66+
print("Original data:", data)
67+
68+
# Convert to hexadecimal
69+
hexed = binascii.hexlify(data)
70+
print("Hexlified:", hexed)
71+
72+
# Convert to a readable string
73+
print("Hex as string:", hexed.decode('ascii'))
74+
```

0 commit comments

Comments
 (0)