Added a signed int256 formatting function
This commit is contained in:
@@ -245,3 +245,42 @@ bool tostring128(const uint128_t *const number,
|
||||
reverseString(out, offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a uint128_t into a string as a signed integer
|
||||
*
|
||||
* @param[in] number the number to format
|
||||
* @param[in] base the radix used in formatting
|
||||
* @param[out] out the output buffer
|
||||
* @param[in] out_length the length of the output buffer
|
||||
* @return whether the formatting was successful or not
|
||||
*/
|
||||
bool tostring128_signed(const uint128_t *const number,
|
||||
uint32_t base,
|
||||
char *const out,
|
||||
uint32_t out_length) {
|
||||
uint128_t max_unsigned_val;
|
||||
uint128_t max_signed_val;
|
||||
uint128_t one_val;
|
||||
uint128_t two_val;
|
||||
uint128_t tmp;
|
||||
|
||||
// showing negative numbers only really makes sense in base 10
|
||||
if (base == 10) {
|
||||
explicit_bzero(&one_val, sizeof(one_val));
|
||||
LOWER(one_val) = 1;
|
||||
explicit_bzero(&two_val, sizeof(two_val));
|
||||
LOWER(two_val) = 2;
|
||||
|
||||
memset(&max_unsigned_val, 0xFF, sizeof(max_unsigned_val));
|
||||
divmod128(&max_unsigned_val, &two_val, &max_signed_val, &tmp);
|
||||
if (gt128(number, &max_signed_val)) // negative value
|
||||
{
|
||||
sub128(&max_unsigned_val, number, &tmp);
|
||||
add128(&tmp, &one_val, &tmp);
|
||||
out[0] = '-';
|
||||
return tostring128(&tmp, base, out + 1, out_length - 1);
|
||||
}
|
||||
}
|
||||
return tostring128(number, base, out, out_length); // positive value
|
||||
}
|
||||
|
||||
@@ -52,5 +52,9 @@ void divmod128(const uint128_t *const l,
|
||||
uint128_t *const div,
|
||||
uint128_t *const mod);
|
||||
bool tostring128(const uint128_t *const number, uint32_t base, char *const out, uint32_t outLength);
|
||||
bool tostring128_signed(const uint128_t *const number,
|
||||
uint32_t base,
|
||||
char *const out,
|
||||
uint32_t out_length);
|
||||
|
||||
#endif // _UINT128_H_
|
||||
|
||||
@@ -248,3 +248,42 @@ bool tostring256(const uint256_t *const number,
|
||||
reverseString(out, offset);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a uint256_t into a string as a signed integer
|
||||
*
|
||||
* @param[in] number the number to format
|
||||
* @param[in] base the radix used in formatting
|
||||
* @param[out] out the output buffer
|
||||
* @param[in] out_length the length of the output buffer
|
||||
* @return whether the formatting was successful or not
|
||||
*/
|
||||
bool tostring256_signed(const uint256_t *const number,
|
||||
uint32_t base,
|
||||
char *const out,
|
||||
uint32_t out_length) {
|
||||
uint256_t max_unsigned_val;
|
||||
uint256_t max_signed_val;
|
||||
uint256_t one_val;
|
||||
uint256_t two_val;
|
||||
uint256_t tmp;
|
||||
|
||||
// showing negative numbers only really makes sense in base 10
|
||||
if (base == 10) {
|
||||
explicit_bzero(&one_val, sizeof(one_val));
|
||||
LOWER(LOWER(one_val)) = 1;
|
||||
explicit_bzero(&two_val, sizeof(two_val));
|
||||
LOWER(LOWER(two_val)) = 2;
|
||||
|
||||
memset(&max_unsigned_val, 0xFF, sizeof(max_unsigned_val));
|
||||
divmod256(&max_unsigned_val, &two_val, &max_signed_val, &tmp);
|
||||
if (gt256(number, &max_signed_val)) // negative value
|
||||
{
|
||||
sub256(&max_unsigned_val, number, &tmp);
|
||||
add256(&tmp, &one_val, &tmp);
|
||||
out[0] = '-';
|
||||
return tostring256(&tmp, base, out + 1, out_length - 1);
|
||||
}
|
||||
}
|
||||
return tostring256(number, base, out, out_length); // positive value
|
||||
}
|
||||
|
||||
@@ -53,5 +53,10 @@ void divmod256(const uint256_t *const l,
|
||||
uint256_t *const div,
|
||||
uint256_t *const mod);
|
||||
bool tostring256(const uint256_t *const number, uint32_t base, char *const out, uint32_t outLength);
|
||||
bool tostring256_signed(const uint256_t *const number,
|
||||
uint32_t base,
|
||||
char *const out,
|
||||
uint32_t out_length);
|
||||
|
||||
|
||||
#endif // _UINT256_H_
|
||||
|
||||
Reference in New Issue
Block a user