108
//In case of a code points is over than U+10000, UTF16 encoding requires 2 bytes of 16 bits binary. We have to subtract 0x10000 from the code points. Now the remaining is “14B62”
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
6
namespace StringBuilderToUTF8
7
{
8
9
public static class StringExtensions
10
{
11
//set for UTF-8 encoding, no errors
12
private static readonly Encoding Utf8Encoder = UTF8Encoding.GetEncoding("UTF-8", new EncoderReplacementFallback(string.Empty), new DecoderExceptionFallback());
13
private static readonly Encoding Utf16Encoder = UTF8Encoding.GetEncoding("UTF-16", new EncoderReplacementFallback(string.Empty), new DecoderExceptionFallback());
14
private static readonly Encoding ISO88591Encoder = UTF8Encoding.GetEncoding("ISO-8859-1", new EncoderReplacementFallback(string.Empty), new DecoderExceptionFallback());
15
16
public static string toUTF8String(this StringBuilder sb) //incomming string is UTF-16
17
{
18
19
byte[] bytesUTF16 = Utf16Encoder.GetBytes(sb.ToString());
20
char[] charsUTF16 = Utf16Encoder.GetChars(bytesUTF16);
21
22
byte[] bytesUTF8 = Utf8Encoder.GetBytes(sb.ToString());
23
char[] charsUTF8 = Utf8Encoder.GetChars(bytesUTF8);
24
Cached Result