// using System; using System.Text; [assembly:CLSCompliant(true)] public class CharacterUtilities { [CLSCompliant(false)] public static ushort ToUTF16(String s) { s = s.Normalize(NormalizationForm.FormC); return Convert.ToUInt16(s[0]); } [CLSCompliant(false)] public static ushort ToUTF16(Char ch) { return Convert.ToUInt16(ch); } // CLS-compliant alternative for ToUTF16(String). public static int ToUTF16CodeUnit(String s) { s = s.Normalize(NormalizationForm.FormC); return (int) Convert.ToUInt16(s[0]); } // CLS-compliant alternative for ToUTF16(Char). public static int ToUTF16CodeUnit(Char ch) { return Convert.ToInt32(ch); } public bool HasMultipleRepresentations(String s) { String s1 = s.Normalize(NormalizationForm.FormC); return s.Equals(s1); } public int GetUnicodeCodePoint(Char ch) { if (Char.IsSurrogate(ch)) throw new ArgumentException("ch cannot be a high or low surrogate."); return Char.ConvertToUtf32(ch.ToString(), 0); } public int GetUnicodeCodePoint(Char[] chars) { if (chars.Length > 2) throw new ArgumentException("The array has too many characters."); if (chars.Length == 2) { if (!Char.IsSurrogatePair(chars[0], chars[1])) throw new ArgumentException("The array must contain a low and a high surrogate."); else return Char.ConvertToUtf32(chars[0], chars[1]); } else { return Char.ConvertToUtf32(chars.ToString(), 0); } } } // public class Example { public static void Main() { } }