Skip to main content

Pythonic CTF-friendly conversions between bytes, hex, integers, arrays, and text.

Project description

hexconv

hexconv is a small, dependency-free Python library for CTF-style conversions between 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("flag").to_hex_array(prefix=True)
# ['0x66', '0x6c', '0x61', '0x67']

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("flag", from_=hx.Text, to=hx.HexString)
# '666c6167'

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.0.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.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hexconv-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7526d20805eeb89d06ee70316470603a285230bbd8bf11c725d8f15d33fd1670
MD5 aa75b859b865fba8e7e4cccff0675c2c
BLAKE2b-256 e5613b70499d1374eb8fbe33e2ab76ebe12e7720157676cb7405841361272637

See more details on using hashes here.

File details

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

File metadata

  • Download URL: hexconv-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 067f8c3736718bc390a70c3a49d47827bac13a456edb53d4cb86e5036a7c640d
MD5 ede066c816d8b26ebbe87c44647e58fe
BLAKE2b-256 e7049f3c6dbe8406e3dd91e48a20d8c52089d85518df8e1c88620ad57e6fa714

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