Skip to main content

Pythonic conversion toolkit for bytes, hex, integers, arrays, binary strings, base64, and text.

Project description

hexconv

hexconv is a small, dependency-free Python conversion toolkit for working with bytes, byte arrays, hex strings, hex arrays, integers, integer arrays, bytes literals, binary strings, base64, and raw text.

The main idea is: explicitly state what you have, then ask for what you want.

import hexconv as hx

hx.from_hex("dead beef").to_bytes()
# b'\xde\xad\xbe\xef'

hx.from_hex("dead beef").bytes
# b'\xde\xad\xbe\xef'

hx.from_bytes_array([0xde, 0xad, 0xbe, 0xef]).to_hex()
# 'deadbeef'

hx.from_bytes_array([0xde, 0xad, 0xbe, 0xef]).hex
# 'deadbeef'

hx.from_text("data").to_hex_array(prefix=True)
# ['0x64', '0x61', '0x74', '0x61']

hx.from_int(0xdeadbeef).to_bytes()
# b'\xde\xad\xbe\xef'

hx.from_int_array([0x1234, 0x5678], width=2).to_hex_array(width=2)
# ['1234', '5678']

Why explicit source helpers?

Some inputs are impossible to infer safely:

"face"

That could be ASCII text (66 61 63 65) or hex bytes (fa ce). So hexconv keeps the safe path explicit:

hx.from_text("face").to_hex()
# '66616365'

hx.from_hex("face").to_bytes()
# b'\xfa\xce'

If you do want convenience heuristics, use from_auto:

hx.from_auto("0xdeadbeef").to_int()
# 3735928559

Converter API

If you prefer a reusable converter object, use marker classes:

conv = hx.Converter(hx.BytesArray, hx.HexArray)
conv([0xde, 0xad, 0xbe, 0xef])
# ['de', 'ad', 'be', 'ef']

hx.convert("data", from_=hx.Text, to=hx.HexString)
# '64617461'

For conversions with options, pass input and output option dictionaries:

conv = hx.Converter(
    hx.DecimalIntArray,
    hx.HexArray,
    input_options={"width": 2},
    output_options={"width": 2, "prefix": True},
)

conv([4660, 22136])
# ['0x1234', '0x5678']

Supported source helpers

  • from_bytes(value)
  • from_bytes_array(values)
  • from_bytes_string(value) for strings like "b'\\xde\\xad'"
  • from_hex(value)
  • from_hex_array(values)
  • from_hex_int(value)
  • from_int(value)
  • from_int_array(values, width=...)
  • from_text(value)
  • from_binary(value)
  • from_base64(value)
  • from_auto(value)

Common output methods

  • to_bytes()
  • to_bytearray()
  • to_bytes_array()
  • to_bytes_string()
  • to_hex(sep="", prefix=False, uppercase=False)
  • to_hex_array(width=1, prefix=False, uppercase=False)
  • to_hex_numbers(width=1)
  • to_int(endian="big", signed=False)
  • to_int_array(width=1, endian="big", signed=False)
  • to_text(encoding="ascii", errors="strict")
  • to_binary(sep="")
  • to_base64()
  • to(format_marker, **options)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexconv-0.1.1.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexconv-0.1.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file hexconv-0.1.1.tar.gz.

File metadata

  • Download URL: hexconv-0.1.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for hexconv-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e01ded1f530aeddbd64c46d923ab1c606e42413e943d75ac7837253a0e13c2fc
MD5 33ee2b6ce5d13891f2af9bd7c301fa2d
BLAKE2b-256 d62fc7cd6e790fbc836b6e7f69c2dd1e744b7f402f7160d240c670cbc8702550

See more details on using hashes here.

File details

Details for the file hexconv-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hexconv-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.6

File hashes

Hashes for hexconv-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 256e3dc78e1bb6224aa9ad239642f90c23935f526ecc7d0f41f521fe14f89328
MD5 c06c5a53d4aa34507faca05ec97f2ddc
BLAKE2b-256 91dc19b2f36b3f36b3975fd495dcae90bf012d6957ce5894d80b3f57b2fdd6e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page