Don't click here unless you want to be banned.

LSL Wiki : Hexadecimal

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org

Hexadecimal

Regular decimal numbers are what's known as base 10: 10 number symbols (0123456789) and make all other numbers out of them. Hexadecimal (or "hex)") is another numbering system, which is useful when coding, because it converts easily to binary. Hex uses 16 symbols (0123456789abcdef) and is therefore a base 16 system. As in all other number systems, such as decimal, adding zeroes to the left side of a number has no effect on its value: 0xF is equal to 0x0F.

Decimal Hex Binary
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111

To figure out the value of a number you do a sum, for example, the number 5043 is
(5 * 1000) + (0 * 100) + (4 * 10) + (3 * 1)
another way of writing this is
(5 * 10^3) + (0 * 10^2) + (4 * 10^1) + (3 * 10^0)

Hexidecimal works the same way except with 16 instead of 10, to figure out the value of, for example 18B3:
(1 * 16^3) + (8 * 16^2) + (B * 16^1) + (3 * 16^0)
Converting this to decimal we have
(1 * 4096) + (8 * 256) + (11 * 16) + (3 * 1)
or 6323.

(Binary numbers work exactly the same way, incidentally, but using powers of 2)

A binary 8-bit byte converts easily to a 2-digit hex number, with the first digit representing the first 4 bits, and the second digit representing the second 4 bits.

So, for example, the binary number: 01101101 becomes 0110 1101, then using this chart we convert this to 6D.



Here's an example for converting up to an 8-digit hex number to decimal (there are several ways to do this, but this should be one of the fast kind):
integer hex2int(string hex) {
    if(llGetSubString(hex,0,1) == "0x")
        return (integer)hex;
    if(llGetSubString(hex,0,0) == "x")
        return (integer)("0"+hex);
    return(integer)("0x"+hex);
}

Here's an example for converting an integer into hexadecimal:
string hexc="0123456789ABCDEF";

string int2hex(integer x) 
{
    integer x0 = x & 0xF;
    string res = llGetSubString(hexc, x0, x0);
    x = (x >> 4) & 0x0FFFFFFF; //otherwise we get infinite loop on negatives.
    while( x != 0 )
    {
        x0 = x & 0xF;
        res = llGetSubString(hexc, x0, x0) + res;
        x = x >> 4;
    } 
    return res;
}

See ExampleNumberConversion for a more sophisticated LSL function.


llMD5String
There is no comment on this page. [Display comments/form]