Difference between revisions of "User talk:Strife Onizuka/int2hex"
Line 4: | Line 4: | ||
: I should probably also note that I've actually been experimenting with the following: | : I should probably also note that I've actually been experimenting with the following: | ||
<lsl>string int2hex(integer int) { | |||
string hex = ""; | string hex = ""; | ||
{ | { |
Revision as of 10:11, 11 April 2011
Efficient Hex
I didn't see any links to this from Efficient Hex, do you think maybe it should be linked in? I've been trying different int2hex functions and have found that this one, while a tiny bit heavier on memory is the fastest due to having no looping and never more than 6 function calls.
-- Haravikk (talk|contribs) 13:09, 2 October 2010 (UTC)
- I should probably also note that I've actually been experimenting with the following:
<lsl>string int2hex(integer int) { string hex = ""; { integer shift = 32; integer nybble; string char = ""; while (shift >= 4) { nybble = (int >> (shift -= 4)) & 0xF; if (nybble > 7) { if (nybble > 11) { if (nybble > 13) { if (nybble > 14) char = "f"; // 15 else char = "e"; // 14 } else { if (nybble > 12) char = "d"; // 13 else char = "c"; // 12 } } else { if (nybble > 9) { if (nybble > 10) char = "b"; // 11 else char = "a"; // 10 } else { if (nybble > 8) char = "9"; // 9 else char = "8"; // 8 } } } else { if (nybble > 3) { if (nybble > 5) { if (nybble > 6) char = "7"; // 7 else char = "6"; // 6 } else { if (nybble > 4) char = "5"; // 5 else char = "4"; // 4 } } else { if (nybble > 1) { if (nybble > 2) char = "3"; // 3 else char = "2"; // 2 } else { if (nybble > 0) char = "1"; // 1 else if (hex != "") char = "0"; // 0 else char = ""; } } }
if (char != "") hex += char;
}
}
if (hex == "") hex = "0";
return (hex = "") + hex;
}</lsl>
Rather oddly it seems to take up even less memory under Mono, and performs nearly as well (it's only about 2% behind yours). The memory is likely accounted for due to Mono's ridiculous overhead on function calls, since this code has none, which'll also be why it can keep up in spite of having a loop doing the work. Anyway, kudos for your neat int2hex function, I think I've finally got to grips which what it's actually doing, very inventive stuff!
-- Haravikk (talk|contribs) 14:42, 2 October 2010 (UTC)