Text To Byte Conversion
Revision as of 23:04, 2 December 2013 by Strife Onizuka (talk | contribs) (This should fix the bug and make it a bit faster in the loop. The weird string addition is to make the rest of the base64 work.)
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Here's a function to pass a string of text and receive a list of bytes. A second function is provided to work in reverse. These methods are especially useful for working with encryption.
Text2Bytes.lsl
<lsl> list text2bytes(string text) {
string base64 = llStringToBase64(text) + "=AAAAA"; list bytes; integer i; integer n = llSubStringIndex(base64, "="); base64 = llDeleteSubString(base64, n, n + 3); integer dword; for(i = 0; i < n; i += 4) bytes += [ ((dword = ((llBase64ToInteger(llGetSubString(base64, i, i + 5)) >> 8) & 0xFFffFF)) >> 16), ((dword >> 8) & 0xFF), (dword & 0xFF) ]; return llList2List(bytes, 0, (-3 >> !!(dword & 0xFF00)) | !!(dword & 0xFF));
}
string bytes2text(list bytes) {
string text = ""; integer i = -1; integer b = 0; while((b = (llList2Integer(bytes, ++i) & 0xFF))) { integer A = (b << 22) & 0x3C000000; integer B = (b << 20) & 0x00F00000; text += "%" + llGetSubString(llIntegerToBase64( A + B + 0xD3400000 - (0xF8000000 * (A / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000 - (0x03E00000 * (B / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000 ), 0, 2); } return llUnescapeURL(text);
}
default {
state_entry() { list bytes = text2bytes("Hello, Avatar!"); llSay(0, llList2CSV(bytes));
string text = bytes2text(bytes); llSay(0, text); }
touch_start(integer total_number) { list bytes = text2bytes("Touched."); llSay(0, llList2CSV(bytes));
string text = bytes2text(bytes); llSay(0, text); }
}
</lsl>