Base Converter¶
Arbitrary precision base conversion by Daniel Gehriger <gehriger@linkcad.com> Permission for use was given here. This has been heavily modified since copying, has been hardcoded for a specific case, then translated to C.
C/C++ Section¶
-
const static char *
base_58
¶ This buffer contains all of the characters within the base_58 “alphabet”
-
const static char *
ascii
¶ This buffer contains all of the characters within the extended ascii “alphabet”
-
static inline size_t
find_base_58
(const char search)¶ This is the equivalent of base_58.indexOf(search)
Parameters: - search – The character you would like to search for
Returns: The index of this character in base_58, or -1
-
static inline unsigned long long
from_base_58
(const char *str, const size_t len)¶ This converts a short base_58 buffer to its ascii equivalent
Parameters: - str – The buffer you wish to convert
- len – The length of the buffer to convert
Returns: The equivalent integral value
-
static inline unsigned int
base2dec
(const char *value, const size_t len)¶ Converts a small ascii buffer to its equivalent integral value
Parameters: - value – The buffer you wish to convert
- len – The length of the buffer to convert
Returns: The equivalent integral value
-
static inline void
dec2base
(unsigned int value, char *result, size_t *len)¶ Converts an integral value to its equivalent binary buffer, then places this in result and updates len
Parameters: - value – The value you wish to convert (as an unsigned int)
- result – The buffer result
- len – The length of the buffer result
Note
This uses
memmove()
to transfer data, so it’s helpful if you start with a larger-than-necessary buffer
-
static char *
to_base_58
(unsigned long long i, size_t *len)¶ Converts an integral value to base_58, then updates len
Parameters: - i – The value you want to convert
- len – The length of the generated buffer
Returns: A buffer containing the base_58 equivalent of
i
Note
The return value needs to have
free()
called on it at some point
-
static unsigned int
divide_58
(char *x, size_t *length)¶ Divides an ascii buffer by 58, and returns the remainder
Parameters: - x – The binary buffer you wish to divide
- length – The length of the buffer
Returns: An unsigned int which contains the remainder of this division
-
static char *
ascii_to_base_58_
(const char *input, size_t length, size_t *res_len)¶ Converts an arbitrary ascii buffer to its base_58 equivalent. The length of this buffer is placed in res_len.
Parameters: - input – An input buffer
- length – The length of said buffer
- res_len – A pointer to the return buffer’s length
Returns: A buffer containing the base_58 equivalent of the provided buffer.
-
static char *
ascii_to_base_58
(const char *input, size_t length, size_t *res_len, size_t minDigits)¶ Converts an arbitrary ascii buffer into its base_58 equivalent. This is largely used for converting hex digests, or other such things which cannot conveniently be converted to an integral.
Parameters: - input – An input buffer
- length – The length of said buffer
- res_len – A pointer to the return buffer’s length
- minDigits – The minimum number of base_58 digits you would like to get back
Returns: A buffer containing the base_58 equivalent of the provided buffer.
C++ Only Section¶
-
static std::string
ascii_to_base_58
(std::string input)¶ Converts an arbitrary binary buffer into its base_58 equivalent.
This is a shortcut version of
ascii_to_base_58()
, with arguments:input.c_str()
input.length()
&size_placeholder
1
Parameters: input – A std::string
which contains the buffer you wish to convertReturns: A std::string
which contains the resulting data
-
static std::string
to_base_58
(unsigned long long i)¶ Converts an integral value into its base_58 equivalent. This takes the data from
to_base_58()
and converts it to astd::string
.Parameters: i – The integral value you wish to convert Returns: A std::string
which contains the resulting data