|
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. |
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); }
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; }