Difference between revisions of "Text To Byte Conversion"
Jump to navigation
Jump to search
(This should work but it is by no means pretty.) |
(→Text2Bytes.lsl: An imperfect hack, but it should fix Babbage's problem.) |
||
Line 21: | Line 21: | ||
{ | { | ||
string text = ""; | string text = ""; | ||
integer i = 0; | integer i = llListFindList(bytes, [0]); | ||
integer n = llGetListLength(bytes); | integer n = llGetListLength(bytes); | ||
for(; i < n; i += 3) | if(~i) | ||
bytes = llDeleteSubList(bytes, n = i, -1) + [32, 32]; | |||
else | |||
bytes += [32, 32]; | |||
for(i = 0; i < n; i += 3) | |||
{ | { | ||
integer dword = | integer dword = | ||
Line 31: | Line 35: | ||
text += llGetSubString(llIntegerToBase64(dword), 0, 3); | text += llGetSubString(llIntegerToBase64(dword), 0, 3); | ||
} | } | ||
return llGetSubString(llBase64ToString(text), 0, n | return llGetSubString(llBase64ToString(text), 0, i - n - 1); | ||
} | } | ||
Revision as of 21:26, 5 January 2010
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); list bytes; integer i; integer n = llStringLength(base64); integer dword; for(i = 0; i < n; i += 4) bytes += [ ((dword = ((llBase64ToInteger(llGetSubString(base64, i, i + 3) + "==") >> 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 = llListFindList(bytes, [0]); integer n = llGetListLength(bytes); if(~i) bytes = llDeleteSubList(bytes, n = i, -1) + [32, 32]; else bytes += [32, 32]; for(i = 0; i < n; i += 3) { integer dword = (llList2Integer(bytes, i) & 0xFF) << 24 | (llList2Integer(bytes, i + 1) & 0xFF) << 16 | (llList2Integer(bytes, i + 2) & 0xFF) << 8; text += llGetSubString(llIntegerToBase64(dword), 0, 3); } return llGetSubString(llBase64ToString(text), 0, i - n - 1);
}
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>