using System;
using System.Text;
namespace EGFramework{
//use this extension,you should add System.Text.Encoding.CodePages package from Nuget
public static class EGEncodingExtension
{
public static bool IsInit{ set; get; }
///
/// get encoding from encoding params(string).
///
///
///
///
public static Encoding GetEncoding(this IEGFramework self,string encodingTxt){
if(!IsInit){
IsInit = true;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
return Encoding.GetEncoding(encodingTxt);
}
///
/// Make a string to bytes with encoding params(string).
///
///
///
///
public static byte[] ToBytesByEncoding(this string self,string encodingTxt){
if(!IsInit){
IsInit = true;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
return Encoding.GetEncoding(encodingTxt).GetBytes(self);
}
///
/// Make a string to bytes with encoding.
///
///
///
///
public static byte[] ToBytesByEncoding(this string self,Encoding encoding){
if(!IsInit){
IsInit = true;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
return encoding.GetBytes(self);
}
///
/// Make a bytes to string with encoding params(string).
///
///
///
///
public static string ToStringByEncoding(this byte[] self,string encodingTxt){
if(!IsInit){
IsInit = true;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
return Encoding.GetEncoding(encodingTxt).GetString(self);
}
///
/// Make a bytes to string with encoding.
///
///
///
///
public static string ToStringByEncoding(this byte[] self,Encoding encoding){
if(!IsInit){
IsInit = true;
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
return encoding.GetString(self);
}
}
}