You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将十六进制字符组成的字符串转换为十六进制整数列表

Got it, let's figure out how to turn that long hex string into a list of hex integers. Here are a couple of simple, effective ways to do this in Python:

Method 1: Slice the String in Chunks and Convert Each

Your input string is made up of consecutive 2-character hex pairs, so we can slice it into those pairs, then convert each pair to an integer using base 16. This is straightforward and easy to read:

hex_string = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
hex_int_list = [int(hex_string[i:i+2], 16) for i in range(0, len(hex_string), 2)]
print(hex_int_list)

This will output a list like [0, 1, 2, 3, ..., 31]—each integer corresponds to the value of the original 2-character hex pair (e.g., '0a' becomes 10, which is the decimal equivalent of hex 0xA).

Method 2: Use Built-in Bytes Conversion (Faster!)

If you're working with longer strings, this method is more efficient because it uses Python's optimized built-in functions. The bytes.fromhex() method directly converts the hex string to a byte object, which we can then turn into a list of integers:

hex_string = '000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'
hex_int_list = list(bytes.fromhex(hex_string))
print(hex_int_list)

This gives exactly the same result as the first method, but runs quicker for large datasets.

Bonus: Get Hex-formatted Strings Instead

If you want the list elements to be displayed as hex strings (like '0x00', '0x01'), just wrap each conversion in the hex() function:

# Using method 1
hex_str_list = [hex(int(hex_string[i:i+2], 16)) for i in range(0, len(hex_string), 2)]
# Using method 2
hex_str_list = [hex(b) for b in bytes.fromhex(hex_string)]

This will produce a list like ['0x00', '0x01', '0x02', ..., '0x1f'].

内容的提问来源于stack exchange,提问作者Bozz369

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:32:33