Difference between revisions of "Ord"

From Second Life Wiki
Jump to navigation Jump to search
(Now that the bug that stopped us from trimming the input at the very beginning is fixed, we can do that.)
m (Add a see also)
Line 52: Line 52:
</lsl>
</lsl>
|also_functions=* [[Chr]]
|also_functions=* [[Chr]]
* [[Combined Library#Unicode functions]]
|cat1=User-Defined Functions
|cat1=User-Defined Functions
|cat2=Examples
|cat2=Examples
}}
}}

Revision as of 10:48, 14 May 2014

Summary

Function: integer Ord( string chr );

Returns an integer with the Unicode code of the first character in chr.

• string chr String containing the Unicode character whose code is to be found

Specification

<lsl>// Ord() function, written by Pedro Oval, 2010-05-28 // This function works by using llEscapeURL to find the corresponding UTF-8 // string then converts it to the Unicode code. In cases where llEscapeURL // doesn't help, a combination of llStringToBase64 and llBase64ToInteger // does the job instead. integer Ord(string chr) {

   if (chr == "")
       return 0;
   chr = llGetSubString(chr, 0, 0);
   string hex = llEscapeURL(chr);
   if (llGetSubString(hex, 0, 0) != "%")
   {
       // Regular character - we can't take advantage of llEscapeURL in this case,
       // so we use llStringToBase64/llBase64ToInteger instead.
       return llBase64ToInteger("AAAA" + llStringToBase64(chr));
   }
   integer b = (integer)("0x" + llGetSubString(hex, 1, 2));
   if (b < 194

Caveats

  • An empty string will return 0.
  • For some "ill" cases, behaviour can vary between using Mono or not.
  • At the time of writing this article, the performance of this function is very low, because the performance of the underlying functions used is also very low. Use it only if there's no alternative.
  • Passing more than 1 character is possible, and in that case only the first character's code will be returned.

Examples

<lsl> integer heart = Ord("♥"); // sets 'heart' to 9829 (0x2665, the Unicode code of the heart) integer space = Ord(" "); // sets 'space' to 32 (which is the ASCII/Unicode code of the space)

</lsl>