Miscellaneous string conversion and parsing functions

depthcharge.string.to_positive_int(string: str, string_desc='', exit_on_fail=True) int

Convert a string to a postive integer and optionally:

  • Print 'Invalid <string_desc> value: <string>' to sys.stderr.

  • Exit the program with a return code value of 2.

depthcharge.string.length_to_int(len_str: str, desc='length', exit_on_fail=True) int

Convert a numeric string with one of the following (case insensitive) length suffixes into its corresponding integer representation.


Multiplication Factor



K or KiB



1,000,000 (1,000 ^ 2)

M or MiB

1,048,576 (1,024 ^ 2)


1,000,000,000 (1,000 ^ 3)

G or GiB

1,073,741,824 (1,024 ^ 3)

depthcharge.string.keyval_list_to_dict(arg_list: list) dict

Helper routine for converting command line argument strings in the form 'key1=val1,key2=val2,...' into a dictionary suitable for passing to a function as **kwargs.

If a key is specified in the string with no value, it is assigned a value of True.

It is the responsibility of the caller or code consuming the diectionary to validate the types and values of the dictionary entries.

depthcharge.string.str_to_property_keyval(arg: str) tuple

Helper routine for converting a command-line argument string in the form:


to a tuple containing a property string and a dictionary:

('<property>', {<key>: <value>, ... })

The returned tuple will contain an empty dictionary if the provided argument does not contain a key-value list.

If keys are provided without corresponding values, they will be assigned a value of True.

depthcharge.string.xxd(address, data: bytes) str

Return the provided data as a hex dump formatted in the style of xxd -g1 <filename>. The provided address value will be the base address in the hex dump.

depthcharge.string.xxd_reverse(hexdump: str) tuple

Convert a well-formed hexdump produced by the xxd() function back into bytes. (i.e., no formatting error recovery is guaranteed.)

Returns a tuple: (address: int, data: bytes)

depthcharge.string.xxd_reverse_file(filename: str) tuple

Load the binary data in the specified file and invoke xxd_reverse() on the contents.

Returns a tuple: (address: int, data: bytes)