Skip to content

Commit

Permalink
Fix to_hex
Browse files Browse the repository at this point in the history
  • Loading branch information
cziter15 committed Nov 19, 2024
1 parent 45ee1d6 commit 135e984
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/ksf/ksConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,22 @@ namespace ksf
@return A string representing the hexadecimal value of the input integer.
*/
inline std::string to_hex(int value)
inline std::string to_hex(int value)
{
std::string hex;
while (value != 0) {
int nibble = value & 0xf;
char c = (nibble < 10) ? ('0' + nibble) : ('a' + (nibble - 10));
hex = c + hex;
value >>= 4;
if (value == 0)
return {"0"};

unsigned int uvalue{static_cast<unsigned int>(value)};
char buffer[8];
int pos{8};
while (uvalue != 0)
{
int nibble{uvalue & 0xf};
buffer[--pos] = (nibble < 10) ? ('0' + nibble) : ('a' + (nibble - 10));
uvalue >>= 4;
}
return hex;

return std::string(buffer + pos, 8 - pos);
}

/*!
Expand Down

0 comments on commit 135e984

Please sign in to comment.