-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBytesExtension.cs
More file actions
38 lines (36 loc) · 1.22 KB
/
BytesExtension.cs
File metadata and controls
38 lines (36 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpExtensions.OpenSource
{
public static class BytesExtension
{
private static readonly List<Encoding> Encodings = Encoding.GetEncodings()
.Select(x => Encoding.GetEncoding(x.Name, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback))
.ToList();
public static string DecodeBytes(this byte[] bytes)
{
foreach (var encoding in Encodings)
{
try
{
return encoding.GetString(bytes);
}
catch { }
}
throw new System.Exception("DecodeBytes - Failed To Decode Bytes");
}
public static byte[] SmartEncoding(this string str)
{
foreach (var encoding in Encodings)
{
try
{
return encoding.GetBytes(str);
}
catch { }
}
throw new System.Exception("DecodeBytes - Failed To Decode Bytes");
}
}
}