An open API service indexing awesome lists of open source software.

https://github.com/foxcapades/renpy-util-hex

Hex utils.
https://github.com/foxcapades/renpy-util-hex

Last synced: over 1 year ago
JSON representation

Hex utils.

Awesome Lists containing this project

README

          

= Ren'Py Hex Utils
:toc:

Provides a collection of utility functions for parsing and rendering hex values
from/to ints or iterables of bytes.

This is intended to be used as part of other projects.

== Functions

All functions are prefixed with `fox_` to avoid conflicts with any other
tooling.

[NOTE]
--
In the following function signatures, arguments suffixed with a `?` character
are optional.
--

=== `fox_int_to_hex(value, min_width?, prefix?, upper?)`

Converts the given int value into a hex string with an optional prefix.

Int values must be greater than or equal to zero.

==== Arguments

`value : int`::
Positive (or zero) int value to convert to a hex string.

`min_width : int`::
Optional, positive int value that specifies the minimum width in characters of
the returned hex string (minus the prefix). If the int value is not large
enough to meet this min width, the the hex string will be left-padded with
zeros.
+
Default = `2`

`prefix : str`::
Optional prefix value that will be prepended onto the returned hex value.
Common examples include `#` and `0x`.
+
Default = `''`

`upper : bool`::
Whether the returned hex string should be uppercased.
+
Default = `False`

==== Returns

`str`::
The generated hex string.

=== `fox_hex_to_int(value, prefix?)`

Converts the given hex string into an int value. Hex string is considered
big-endian.

==== Arguments

`value : str`::
Hex string that will be parsed into an int value.

`prefix : str`::
Prefix that will be stripped off the given value before it is parsed.

==== Returns

`int`::
The int value parsed from the given hex string.

=== `fox_ubytes_to_hex(bytes, prefix?, upper?)`

Takes the given list, tuple, or other iterable and translates the ubyte values
fetched from that iterable into a singular hex string.

The iterable is expected to return the ubytes in big-endian order.

==== Arguments

`bytes : any`::
A list, tuple, or other iterable type from which ubyte values will be retrieved.

`prefix : str`::
Optional prefix that will be prepended to the output hex string. Common
examples include `#` and `0x`.
+
Default = `''`

`upper : bool`::
Flag indicating whether the returned hex should be uppercase.
+
Default = `False`

==== Returns

`str`::
A hex string build for the bytes retrieved from the given list, tuple, or other
iterable.

=== `fox_hex_to_ubytes(value, prefix?)`

Converts the given hex string into a list of ubyte values.

==== Arguments

`value : str`::
Hex string that will be parsed into a list of ubytes.

`prefix : str`::
Prefix that will be stripped off of the given value before it is parsed.

==== Returns

`list[int]`::
A list of ubyte values parsed from the given hex string.

=== `fox_ubyte_to_hex(byte, upper?)`

Converts the given ubyte value to a hex character pair in a string.

==== Arguments

`byte : int`::
UByte value to convert to hex. This value must be between `0` and `255`
(inclusive) or an exception will be raised.

`upper : bool`::
Flag indicating whether the returned hex should be uppercase.
+
Default = `False`

==== Returns

`str`::
A 2 character hex string representing the given ubyte value.

=== `fox_hex_is_valid(hex)`

Tests whether the given value is a hex string. Assumes that any prefixes have
been removed before testing.

==== Arguments

`hex : str`::
Value to test.

==== Returns

`bool`::
Whether the given value was a valid hex string.