Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include "trf.h"
  3.  
  4. #define ascii_alphanum_base 48
  5.  
  6. char* trf::string::int2hex(unsigned int outputdigits, long long input)
  7. {
  8.     //sanity checking and warnings
  9.     if (outputdigits == 0 || input == NULL)
  10.     {
  11.         printf("ERROR: bad output length or input: int2hex(outputdigits: %i, input: %lli) \n", outputdigits, input);
  12.         return nullptr;
  13.     }
  14.     if (outputdigits < sizeof(input))
  15.     {
  16.         printf("WARNING: output size smaller than input, result might be incorrect \n \n");
  17.     }
  18.  
  19.     char *nibbles = new char[outputdigits];
  20.     int shiftoffset;
  21.  
  22.     //deflate the input into its constituent nibbles and convert them to ASCII letters,
  23.     //taking only the desired amount of nibbles specified in outputdigits
  24.     for (int currentnibble = outputdigits - 1; currentnibble >= 0; currentnibble--)
  25.     {
  26.         shiftoffset = 4 * ((outputdigits-1) - currentnibble);
  27.         nibbles[currentnibble] = (char)((input >> shiftoffset) & 15) + ascii_alphanum_base;
  28.         //ascii numbers and letters arent contiguous so a small fix needs to be done for A-F
  29.         if (nibbles[currentnibble] > ascii_alphanum_base + 9)
  30.         {
  31.             nibbles[currentnibble] += 7;
  32.         }
  33.     }
  34.  
  35.     return nibbles;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement