gmlscripts

dec_to_hex

Apr 21st, 2020
1,788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @func   dec_to_hex(dec [, digits])
  2. ///
  3. /// @desc   Returns a string of hexadecimal digits.
  4. ///         Hexadecimal strings are padded to an
  5. ///         optional number of digits.
  6. ///
  7. /// @arg    {real}      dec         non-negative integer
  8. /// @arg    {real}      digits      minimum number of digits
  9. ///
  10. /// @return {string}    hexadecimal
  11. ///
  12. /// GMLscripts.com/license
  13. {
  14.     var dec = argument[0],
  15.         dig = (argument_count > 1) ? argument[1] : 1,
  16.         hex = "",
  17.         h = "0123456789ABCDEF";
  18.        
  19.     while (dig-- || dec) {
  20.         hex = string_char_at(h, (dec & $F) + 1) + hex;
  21.         dec = dec >> 4;
  22.     }
  23.    
  24.     return hex;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment