{"id":1505,"library":"hexbytes","title":"HexBytes: Python `bytes` subclass for hex representation","description":"HexBytes is a thin wrapper around the Python built-in `bytes` class (version 1.3.1). It extends `bytes` by accepting a wider range of initialization types including `bool`, `int`, `str`, `bytearray`, and `memoryview`. Key features include a 0x-prefixed console representation (`__repr__`) and a `to_0x_hex()` method for consistent 0x-prefixed hexadecimal string output. The library is actively maintained with regular releases.","status":"active","version":"1.3.1","language":"python","source_language":"en","source_url":"https://github.com/ethereum/hexbytes","tags":["hex","bytes","ethereum","data-conversion","utility"],"install":[{"cmd":"pip install hexbytes","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"HexBytes","correct":"from hexbytes import HexBytes"}],"quickstart":{"code":"from hexbytes import HexBytes\n\n# Convert from bytes to a prettier representation\nhb_from_bytes = HexBytes(b\"\\x03\\x08wf\\xbfh\\xe7\\x86q\\xd1\\xeaCj\\xe0\\x87\\xdat\\xa1'a\\xda\\xc0 \\x01\\x1a\\x9e\\xdd\\xc4\\x90\\x0b\\xf1;\")\nprint(f\"From bytes: {hb_from_bytes}\")\n\n# HexBytes accepts hex string representation (ignoring case and 0x prefixes)\nhb_from_hex_str = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')\nprint(f\"From hex string: {hb_from_hex_str}\")\n\n# Get the 0x-prefixed hex string\nhex_string_output = hb_from_hex_str.to_0x_hex()\nprint(f\"Using to_0x_hex(): {hex_string_output}\")\n\n# Cast back to basic bytes type\noriginal_bytes = bytes(hb_from_hex_str)\nprint(f\"Cast back to bytes: {original_bytes}\")","lang":"python","description":"Initialize HexBytes from various types and retrieve different representations."},"warnings":[{"fix":"Ensure your Python environment is 3.8 or newer for HexBytes 1.x. For older Python versions, use an older HexBytes release (e.g., <0.3.0 for Python 3.7 compatibility).","message":"HexBytes has dropped support for older Python versions. Versions prior to 0.2.0 dropped Python 3.5, versions prior to 0.3.0 dropped Python 3.6, and current 1.x series (since 1.0.0) requires Python >= 3.8. Attempting to install on unsupported versions will fail or lead to unexpected behavior.","severity":"breaking","affected_versions":"<1.0.0 (Python 3.5, 3.6), >=1.0.0 (Python <3.8)"},{"fix":"To consistently get a 0x-prefixed hex string, always use the `my_hexbytes.to_0x_hex()` method.","message":"While HexBytes' `__repr__` method provides a convenient 0x-prefixed hex string for console output, it does not override the standard `bytes.hex()` or `__str__` methods. Calling `str(my_hexbytes)` or `my_hexbytes.hex()` will revert to the default `bytes` behavior (e.g., `b'\\x01\\x02'` or `'0102'`).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0x':46,54,60 '0x-prefixed':45,59 '1.3.1':22 'accept':27 'activ':68 'around':13 'bool':35 'built':17 'built-in':16 'byte':3,19,25,74 'bytearray':38 'class':20 'consist':58 'consol':48 'convers':78 'data':77 'data-convers':76 'ethereum':75 'extend':24 'featur':42 'hex':6,55,73 'hexadecim':62 'hexbyt':1,8 'includ':34,43 'initi':32 'int':36 'key':41 'librari':66 'maintain':69 'memoryview':40 'method':56 'output':64 'prefix':47,61 'python':2,15 'rang':30 'regular':71 'releas':72 'repr':50 'represent':7,49 'str':37 'string':63 'subclass':4 'thin':11 'type':33 'util':79 'version':21 'wider':29 'wrapper':12","created_at":"2026-04-09T03:50:49.571725+00:00","updated_at":"2026-04-17T14:17:59.105294+00:00","problems":[{"fix":"Install the 'hexbytes' library using pip: 'pip install hexbytes'.","cause":"The 'hexbytes' library is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'hexbytes'"},{"fix":"Use the 'to_0x_hex()' method to get a '0x'-prefixed hexadecimal string: 'hb.to_0x_hex()'.","cause":"The 'hex' method in 'HexBytes' returns a hexadecimal string without the '0x' prefix.","error":"AttributeError: 'HexBytes' object has no attribute 'hex'"},{"fix":"Ensure that 'HexBytes' instances are not used as callable functions; use methods like 'to_0x_hex()' instead.","cause":"Attempting to call a 'HexBytes' object as a function, which is not supported.","error":"TypeError: 'HexBytes' object is not callable"},{"fix":"Initialize 'HexBytes' with a hexadecimal string without the '0x' prefix: 'hb = HexBytes('03087766BF68E78671D1EA436AE087DA74A12761DAC020011A9EDDC4900BF13B')'.","cause":"Passing a '0x'-prefixed string to 'HexBytes' without removing the prefix.","error":"ValueError: invalid literal for int() with base 16: '0x'"},{"fix":"Convert 'HexBytes' to a 'bytes' object before indexing: 'bytes(hb)[0]'.","cause":"Attempting to access elements of a 'HexBytes' object using indexing, which is not supported.","error":"TypeError: 'HexBytes' object is not subscriptable"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"2.0.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/ethereum/hexbytes","docs":null,"changelog":null,"pypi":"https://pypi.org/project/hexbytes/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}