Difference between revisions of "Text To Byte Conversion"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> tag to <source>)
 
(One intermediate revision by one other user not shown)
Line 4: Line 4:
== Text2Bytes.lsl ==
== Text2Bytes.lsl ==


<lsl>
<source lang="lsl2">
list text2bytes(string text)
list text2bytes(string text)
{
{
     string base64 = llStringToBase64(text) + "=AAAAA";
     string base64 = llStringToBase64(text);
     list bytes;
     list bytes;
     integer i;
     integer i;
     integer n = llSubStringIndex(base64, "=");
     integer n = llStringLength(base64);
    base64 = llDeleteSubString(base64, n, n + 3);
     integer dword;
     integer dword;
     for(i = 0; i < n; i += 4)
     for(i = 0; i < n; i += 4)
         bytes += [  ((dword = ((llBase64ToInteger(llGetSubString(base64, i, i + 5)) >>  8) & 0xFFffFF)) >> 16),  
         bytes += [  ((dword = ((llBase64ToInteger(llGetSubString(base64, i, i + 3) + "==") >>  8) & 0xFFffFF)) >> 16),  
                     ((dword >> 8) & 0xFF), (dword & 0xFF) ];
                     ((dword >> 8) & 0xFF), (dword & 0xFF) ];
     return llList2List(bytes, 0, (-3 >> !!(dword & 0xFF00)) | !!(dword & 0xFF));
     return llList2List(bytes, 0, (-3 >> !!(dword & 0xFF00)) | !!(dword & 0xFF));
Line 58: Line 57:
}
}


</lsl>
</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

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