User talk:Strife Onizuka/int2hex

From Second Life Wiki
Jump to: navigation, 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! --
-- Haravikk (talk|contribs) 13:49, 20 August 2013 (PDT)
That is pretty cool. I don't recall seeing it before. You know me, I can't leave anything alone. I've rebalanced things so it will be about the same (a bit faster for "A" and "B"). I added an extra loop to eat through the null states. I made zero a special case because it allowed me to avoid a race condition caused by said new loop. I'm not sure if I would have thought of adding a variable to avoid the cost of the repeated "+=", that is truly inspired. -- Strife (talk|contribs) 23:14, 20 August 2013 (PDT)

<lsl>string int2hex(integer int) { string hex = ""; if (int) { integer shift = 32; integer nybble; string char = ""; do ; while (!((int >> (shift -= 4)) & 0xF)); do { if (10 < (nybble = ((int >> shift) & 0xF))) { 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 char = "B"; // 11 } } else { if (9 < nybble) char = "A"; // 10 else char = (string)nybble; // 0-9 } hex += char; } while ((shift -= 4) >= 0); } else { hex = "0";

       }

return (hex = "") + hex; }</lsl>