ID:195063
 
//Title: dec2hex
//Credit to: Popisfizzy

/*
Simple procedure to convert hexadecimal into decimal,
using bitwise math. 'e' means 'expanded'. By default, this
is false, meaning that only the relevant digits will be
shown. If it is true, it will be extended to 4 digits.

*Note: This version will only work for digits less
than or equal to sixteen bits in length.
*/


proc/dec2hex(n, e = 0)
for(var/a = 0, (e ? (a < 16) : ((2 ** a) <= n)), a += 4)
var/b = (n >> a) & 15
. = "[b < 10 ? b : (b != 0 && ascii2text(65 + (b - 10)))][.]"
if(!.) return "0"

//Test Code:

mob/verb/unexpanded_dec2hex(n as num)
world << "[n] (base 10) = [dec2hex(n)] (base 16)"

mob/verb/expanded_dec2hex(n as num)
world << "[n] (base 10) = [dec2hex(n, 1)] (base 16, expanded)"