[go: up one dir, main page]

Menu

[897c37]: / mlib / lib / hex.lpc  Maximize  Restore  History

Download this file

53 lines (47 with data), 722 Bytes

/*
 * lib/hex.c
 * Copyright Shattered Worlds mudlib
 *
 * Hexadecimal to/from Integer functions
 */

static string HexDigit; /* so this won't get corrupted */

reset(arg) 
{
    if (!arg) 
    {	
	HexDigit= "0123456789abcdef";
    }
}

(int|string) hex2int(string hex) 
{
int tot, mult, i, j;
	mult = 1;
	tot = 0;
	i = sizeof(hex)-1;
	while (i >= 0)
	{
		if ((j = index(HexDigit, hex[i..i])) < 0) 
			return hex;
		tot += j * mult;
		mult = mult * 16;
		i --;
	}
	return tot;
}

string int2hex(int hex) {
string ret;
int i;
int neg;
	ret = "";
	if(hex < 0) hex = -hex;
	while (hex)
	{
		ret = HexDigit[hex%16..hex%16] + ret;
		hex = hex / 16;
	}
	if (ret == "")
		ret = "0";
	if (neg)
		ret = "-" + ret;
	return ret;
}