Difference between revisions of "Text To Byte Conversion"
Jump to navigation
Jump to search
m (LOL typo) |
m (<lsl> tag to <source>) |
||
Line 4: | Line 4: | ||
== Text2Bytes.lsl == | == Text2Bytes.lsl == | ||
< | <source lang="lsl2"> | ||
list text2bytes(string text) | list text2bytes(string text) | ||
{ | { | ||
Line 57: | Line 57: | ||
} | } | ||
</ | </source> | ||
{{LSLC|Examples|Text To Byte Conversion}} | {{LSLC|Examples|Text To Byte Conversion}} | ||
{{LSLC|User-Defined_Functions}} | {{LSLC|User-Defined_Functions}} | ||
[[Category: LSL Encryption]] | [[Category: LSL Encryption]] |
Latest revision as of 15:40, 22 January 2015
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
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 = -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, 1);
}
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);
}
}