User talk:Strife Onizuka/int2hex

From Second Life Wiki
Jump to navigation Jump to search

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 (9 < nybble) { if (12 < nybble) { if (14 < nybble) char = "F"; // 15 else if (13 < nybble) char = "E"; // 14 else char = "D"; // 13 } else { if (11 < nybble) char = "C"; // 12 else if (10 < nybble) char = "B"; // 11 else char = "A"; // 10 } } else { if ((0 == nybble) && ("" == hex)) char = ""; else char = (string)nybble; // 0-9 }

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)

Saw you added a note to the main page and remembered I'd posted the above code, which I've tweaked to what I'm actually using; I have no idea why I originally had branches for 0-9 when I could just cast those!