Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| // <Snippet4> | |
| using System; | |
| using System.Text; | |
| public class Example | |
| { | |
| public static void Main() | |
| { | |
| Encoding enc = Encoding.GetEncoding("us-ascii", | |
| new EncoderExceptionFallback(), | |
| new DecoderExceptionFallback()); | |
| string str1 = "\u24C8 \u2075 \u221E"; | |
| Console.WriteLine(str1); | |
| foreach (var ch in str1) | |
| Console.Write("{0} ", Convert.ToUInt16(ch).ToString("X4")); | |
| Console.WriteLine("\n"); | |
| // Encode the original string using the ASCII encoder. | |
| byte[] bytes = {}; | |
| try { | |
| bytes = enc.GetBytes(str1); | |
| Console.Write("Encoded bytes: "); | |
| foreach (var byt in bytes) | |
| Console.Write("{0:X2} ", byt); | |
| Console.WriteLine(); | |
| } | |
| catch (EncoderFallbackException e) { | |
| Console.Write("Exception: "); | |
| if (e.IsUnknownSurrogate()) | |
| Console.WriteLine("Unable to encode surrogate pair 0x{0:X4} 0x{1:X3} at index {2}.", | |
| Convert.ToUInt16(e.CharUnknownHigh), | |
| Convert.ToUInt16(e.CharUnknownLow), | |
| e.Index); | |
| else | |
| Console.WriteLine("Unable to encode 0x{0:X4} at index {1}.", | |
| Convert.ToUInt16(e.CharUnknown), | |
| e.Index); | |
| return; | |
| } | |
| Console.WriteLine(); | |
| // Decode the ASCII bytes. | |
| try { | |
| string str2 = enc.GetString(bytes); | |
| Console.WriteLine("Round-trip: {0}", str1.Equals(str2)); | |
| if (! str1.Equals(str2)) { | |
| Console.WriteLine(str2); | |
| foreach (var ch in str2) | |
| Console.Write("{0} ", Convert.ToUInt16(ch).ToString("X4")); | |
| Console.WriteLine(); | |
| } | |
| } | |
| catch (DecoderFallbackException e) { | |
| Console.Write("Unable to decode byte(s) "); | |
| foreach (byte unknown in e.BytesUnknown) | |
| Console.Write("0x{0:X2} "); | |
| Console.WriteLine("at index {0}", e.Index); | |
| } | |
| } | |
| } | |
| // The example displays the following output: | |
| // Ⓢ ⁵ ∞ | |
| // 24C8 0020 2075 0020 221E | |
| // | |
| // Exception: Unable to encode 0x24C8 at index 0. | |
| // </Snippet4> |