Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "trf.h"
- #define ascii_alphanum_base 48
- char* trf::string::int2hex(unsigned int outputdigits, long long input)
- {
- //sanity checking and warnings
- if (outputdigits == 0 || input == NULL)
- {
- printf("ERROR: bad output length or input: int2hex(outputdigits: %i, input: %lli) \n", outputdigits, input);
- return nullptr;
- }
- if (outputdigits < sizeof(input))
- {
- printf("WARNING: output size smaller than input, result might be incorrect \n \n");
- }
- char *nibbles = new char[outputdigits];
- int shiftoffset;
- //deflate the input into its constituent nibbles and convert them to ASCII letters,
- //taking only the desired amount of nibbles specified in outputdigits
- for (int currentnibble = outputdigits - 1; currentnibble >= 0; currentnibble--)
- {
- shiftoffset = 4 * ((outputdigits-1) - currentnibble);
- nibbles[currentnibble] = (char)((input >> shiftoffset) & 15) + ascii_alphanum_base;
- //ascii numbers and letters arent contiguous so a small fix needs to be done for A-F
- if (nibbles[currentnibble] > ascii_alphanum_base + 9)
- {
- nibbles[currentnibble] += 7;
- }
- }
- return nibbles;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement