23 public static Encoding
GetEncoding(
byte[] Data, Encoding DefaultEncoding, out
int BomLength)
25 return GetEncoding(Data, 0, Data.Length, DefaultEncoding, out BomLength);
42 public static Encoding
GetEncoding(
byte[] Data,
int Offset,
int Count, Encoding DefaultEncoding, out
int BomLength)
71 if (Count >= 3 && Data[Offset] == 0xef && Data[Offset + 1] == 0xbb && Data[Offset + 2] == 0xbf)
76 else if (Count >= 2 && Data[Offset] == 0xfe && Data[Offset + 1] == 0xff)
79 return Encoding.BigEndianUnicode;
81 else if (Count >= 2 && Data[Offset] == 0xff && Data[Offset + 1] == 0xfe)
83 if (Count >= 4 && Data[Offset + 2] == 0 && Data[Offset + 3] == 0)
86 return Encoding.UTF32;
91 return Encoding.Unicode;
94 else if (Count >= 4 && Data[Offset] == 0 && Data[Offset + 1] == 0 && Data[Offset + 2] == 0xfe && Data[Offset + 3] == 0xff)
99 else if (Count >= 4 && Data[Offset] == 0x2b && Data[Offset + 1] == 0x2f && Data[Offset + 2] == 0x76)
101 if (Data[Offset + 3] == 0x39 ||
102 Data[Offset + 3] == 0x2b ||
103 Data[Offset + 3] == 0x2f)
106 return Encoding.UTF7;
108 else if (Data[Offset + 3] == 0x38)
110 if (Count >= 5 && Data[Offset + 4] == 0x2d)
115 return Encoding.UTF7;
118 else if (Count >= 3 && Data[Offset] == 0xf7 && Data[Offset + 1] == 0x64 && Data[Offset + 2] == 0x4c)
119 throw new ArgumentException(
"UTF-1 encoding not supported.", nameof(Data));
120 else if (Count >= 4 && Data[Offset] == 0xdd && Data[Offset + 1] == 0x73 && Data[Offset + 2] == 0x66 && Data[Offset + 3] == 0x73)
121 throw new ArgumentException(
"UTF-EBCDIC encoding not supported.", nameof(Data));
122 else if (Count >= 3 && Data[Offset] == 0x0e && Data[Offset + 1] == 0xfe && Data[Offset + 2] == 0xff)
123 throw new ArgumentException(
"SCSU encoding not supported.", nameof(Data));
124 else if (Count >= 3 && Data[Offset] == 0xfb && Data[Offset + 1] == 0xee && Data[Offset + 2] == 0x28)
125 throw new ArgumentException(
"BOCU encoding not supported.", nameof(Data));
126 else if (Count >= 4 && Data[Offset] == 0x84 && Data[Offset + 1] == 0x31 && Data[Offset + 2] == 0x95 && Data[Offset + 3] == 0x33)
148 public static string GetString(
byte[] Data,
int Offset,
int Count, Encoding DefaultEncoding)
153 Encoding Encoding =
GetEncoding(Data, Offset, Count, DefaultEncoding, out
int BomLength);
155 return Encoding.GetString(Data, Offset + BomLength, Count - BomLength);
169 public static string GetString(
byte[] Data, Encoding DefaultEncoding)
174 Encoding Encoding =
GetEncoding(Data, DefaultEncoding, out
int Offset);
176 return Encoding.GetString(Data, Offset, Data.Length - Offset);
186 if (iso_8859_1 is
null)
187 iso_8859_1 = Encoding.GetEncoding(
"iso-8859-1");
193 private static Encoding iso_8859_1 =
null;
202 if (utf_32be is
null)
203 utf_32be = Encoding.GetEncoding(
"utf-32BE");
209 private static Encoding utf_32be =
null;
219 gb18030 = Encoding.GetEncoding(
"GB18030");
225 private static Encoding gb18030 =
null;
234 if (utf8WithBom is
null)
235 utf8WithBom =
new UTF8Encoding(
true);
241 private static Encoding utf8WithBom =
null;
250 if (utf8WithoutBom is
null)
251 utf8WithoutBom =
new UTF8Encoding(
false);
253 return utf8WithoutBom;
257 private static Encoding utf8WithoutBom =
null;
271 if (s.StartsWith(
"["))
273 i = s.LastIndexOf(
"]:");
281 i = s.LastIndexOf(
':');
286 if (
int.TryParse(s.Substring(i + 1), out
int j) && j >= 0 && j <= 65535)
287 return s.Substring(0, i);
Static class managing binary representations of strings.
static Encoding Utf8WithoutBom
UTF-8 encoding without Byte Order Mark (BOM)
static Encoding BigEndianUnicode32
ISO-8859-1 encoding.
static Encoding ISO_8859_1
ISO-8859-1 encoding.
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
static Encoding GetEncoding(byte[] Data, Encoding DefaultEncoding, out int BomLength)
Gets the encoding of a string from a its binary representation, taking any Byte Order Mark (BOM) into...
static Encoding GetEncoding(byte[] Data, int Offset, int Count, Encoding DefaultEncoding, out int BomLength)
Gets the encoding of a string from a its binary representation, taking any Byte Order Mark (BOM) into...
static Encoding Utf8WithBom
UTF-8 encoding with Byte Order Mark (BOM)
static Encoding GB18030
GB18030 encoding (simplified Chinese).
static string RemovePortNumber(this string s)
Returns the port number at the end of a string, if found.
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.