Difference between revisions of "String2Hex"
Omei Qunhua (talk | contribs) m (→Full Unicode Support: use -> used. Still not fully happy with the wording of the comment.) |
|||
Line 8: | Line 8: | ||
string String2Hex(string str){ | string String2Hex(string str){ | ||
string b64 = (string)llParseString2List(llStringToBase64(str), ["="], []); | string b64 = (string)llParseString2List(llStringToBase64(str), ["="], []); | ||
integer length = (llStringLength(b64) * 3) >> 2; | integer length = ((llStringLength(b64) * 3) >> 1) & -2; | ||
b64 += "AAAAAAA"; // we will trim these off in the end, this works around SVC-104. | b64 += "AAAAAAA"; // we will trim these off in the end, this works around SVC-104. | ||
integer count = -3; | integer count = -3; | ||
integer position = -4; | integer position = -4; | ||
string out; | string out; | ||
while ((count += | while ((count += 6) < length) { | ||
integer I = llBase64ToInteger(llGetSubString(b64, position += 4, position + 7)); | integer I = llBase64ToInteger(llGetSubString(b64, position += 4, position + 7)) | 1;//This is a bottle neck | ||
string T = ""; | |||
do { | |||
integer A = (I >> 2) & 0x3C000000; //28 -> 26 | |||
integer B = (I >> 4) & 0x00F00000; //24 -> 20 | |||
integer C = (I >> 6) & 0x0003C000; //20 -> 14 | |||
T += llGetSubString( //ABC | |||
llIntegerToBase64( | llIntegerToBase64( | ||
A + B + C + 0xD34D0000 | A + B + C + 0xD34D0000 | ||
Line 32: | Line 26: | ||
- (0x03E00000 * (B / 0x00A00000)) //lowercase=0x02400000, uppercase=0x03E00000 | - (0x03E00000 * (B / 0x00A00000)) //lowercase=0x02400000, uppercase=0x03E00000 | ||
- (0x000F8000 * (C / 0x00028000)) //lowercase=0x00090000, uppercase=0x000F8000 | - (0x000F8000 * (C / 0x00028000)) //lowercase=0x00090000, uppercase=0x000F8000 | ||
), | ), 0, 3); | ||
} while (((I = (I << 12)) & 0x1EE7); | |||
out += T;//This is a bottle neck | |||
} | } | ||
return llGetSubString(out, 0, | return llGetSubString(out, 0, length - 1); | ||
} | } | ||
Revision as of 11:20, 14 March 2014
Full Unicode Support
Uses UTF-8 encoding of characters. If you can get the character into a string in LSL, this will encode that character in hex.
For performance reasons you should really just get used to using Base64. Since LSL has no hex functions, implementing them comes at a great performance cost.
<lsl> string String2Hex(string str){
string b64 = (string)llParseString2List(llStringToBase64(str), ["="], []); integer length = ((llStringLength(b64) * 3) >> 1) & -2; b64 += "AAAAAAA"; // we will trim these off in the end, this works around SVC-104. integer count = -3; integer position = -4; string out; while ((count += 6) < length) { integer I = llBase64ToInteger(llGetSubString(b64, position += 4, position + 7)) | 1;//This is a bottle neck string T = ""; do { integer A = (I >> 2) & 0x3C000000; //28 -> 26 integer B = (I >> 4) & 0x00F00000; //24 -> 20 integer C = (I >> 6) & 0x0003C000; //20 -> 14 T += llGetSubString( //ABC llIntegerToBase64( A + B + C + 0xD34D0000 - (0xF8000000 * (A / 0x28000000)) //lowercase=0x90000000, uppercase=0xF8000000 - (0x03E00000 * (B / 0x00A00000)) //lowercase=0x02400000, uppercase=0x03E00000 - (0x000F8000 * (C / 0x00028000)) //lowercase=0x00090000, uppercase=0x000F8000 ), 0, 3); } while (((I = (I << 12)) & 0x1EE7); out += T;//This is a bottle neck } return llGetSubString(out, 0, length - 1);
}
string Hex2String(string hex) {
integer length = (llStringLength(hex) + 1) & -2; while(length) hex = llInsertString(hex, length -= 2, "%"); return llUnescapeURL(hex); //lol
} </lsl>
Limited ASCII-7 Support
These functions only works for ASCII-7 non-control characters.
<lsl> string c_chr=" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
// http://wiki.secondlife.com/wiki/Int2Hex
string int2hex(integer x){
string toret;
do{
toret=llGetSubString("0123456789ABCDEF",x&0x0000000F,x&0x0000000F)+toret;
}while (x=x>>4&0x0FFFFFFF);
return toret;
}
string str2hex(string str){ string ret=""; integer strlen=llStringLength(str); integer i=0; for(;i<strlen;++i){ ret+=int2hex(32+llSubStringIndex(c_chr,llGetSubString(str,i,i))); } return ret; }
string hex2str(string str){ string ret=""; integer strlen=llStringLength(str); integer i=0;integer index; for(;i<strlen;i+=2){ index=((integer)("0x"+llGetSubString(str,i,i+1)))-32; ret+=llGetSubString(c_chr,index,index); } return ret; }
default{ state_entry(){ llOwnerSay(str2hex("test ~")); llOwnerSay(hex2str("74657374207E")); } } </lsl>