public string getString(byte[] inputBytes) { String res = null; String encoding = Global.GLOBAL_ENCODING; AvailableEncoding aEncoding = AvailableEncodingUtils.getAvailableEncoding(encoding, this.error); if (this.HasError()) { return(""); } String encodingString = AvailableEncodingUtils.valueOf(aEncoding); try { res = AvailableEncodingUtils.encapsulateGetString(inputBytes, aEncoding, this.error).Replace("[\ufffd]", ""); res = res.Replace("\x00", string.Empty); if (this.HasError()) { return(""); } } catch (Exception e) { this.error.setError("EU002", e.Message); return(""); } this.error.cleanError(); return(res.Trim()); }
public static string valueOf(AvailableEncoding availableEncoding) { switch (availableEncoding) { case AvailableEncoding.UTF_8: return("UTF-8"); case AvailableEncoding.UTF_16: return("UTF-16"); case AvailableEncoding.UTF_16BE: return("UTF-16BE"); case AvailableEncoding.UTF_16LE: return("UTF-16LE"); case AvailableEncoding.UTF_32: return("UTF-32"); case AvailableEncoding.UTF_32BE: return("UTF-32BE"); case AvailableEncoding.UTF_32LE: return("UTF-32LE"); case AvailableEncoding.SJIS: return("Shift_JIS"); case AvailableEncoding.GB2312: return("GB2312"); default: return(""); } }
public byte[] getBytes(string inputText) { byte[] output = null; String encoding = Global.GLOBAL_ENCODING; AvailableEncoding aEncoding = AvailableEncodingUtils.getAvailableEncoding(encoding, this.error); if (this.HasError()) { return(null); } String encodingString = AvailableEncodingUtils.valueOf(aEncoding); try { output = AvailableEncodingUtils.encapsulateeGetBytes(inputText, aEncoding, this.error); if (this.HasError()) { return(null); } } catch (Exception e) { this.error.setError("EU001", e.Message); return(null); } this.error.cleanError(); return(output); }
public static byte[] encapsulateeGetBytes(string input, AvailableEncoding availableEncoding, Error error) { const string strUniRepChr = "�"; //Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) switch (availableEncoding) { case AvailableEncoding.UTF_8: return(Encoding.UTF8.GetBytes(input)); #if NETCORE case AvailableEncoding.UTF_16: Encoding utf16 = Encoding.GetEncoding(1201, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(utf16.GetBytes(input)); case AvailableEncoding.UTF_16BE: Encoding utf16_be = Encoding.GetEncoding(1201, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(utf16_be.GetBytes(input)); case AvailableEncoding.UTF_16LE: Encoding utf16_le = Encoding.GetEncoding(1200, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(utf16_le.GetBytes(input)); #else case AvailableEncoding.UTF_16: return(Encoding.BigEndianUnicode.GetBytes(input)); case AvailableEncoding.UTF_16BE: return(Encoding.BigEndianUnicode.GetBytes(input)); case AvailableEncoding.UTF_16LE: return(Encoding.Unicode.GetBytes(input)); #endif case AvailableEncoding.UTF_32: Encoding cpUTF32_1 = Encoding.GetEncoding(12001, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(cpUTF32_1.GetBytes(input)); case AvailableEncoding.UTF_32BE: Encoding cpUTF32_2 = Encoding.GetEncoding(12001, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(cpUTF32_2.GetBytes(input)); case AvailableEncoding.UTF_32LE: Encoding cpUTF32_3 = Encoding.GetEncoding(12000, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(cpUTF32_3.GetBytes(input)); case AvailableEncoding.SJIS: #if NETCORE Encoding sjis = CodePagesEncodingProvider.Instance.GetEncoding(932); //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); // Encoding sjis = Encoding.GetEncoding("Shift_JIS"); return(sjis.GetBytes(input)); #else Encoding sjis = Encoding.GetEncoding(932); return(sjis.GetBytes(input)); #endif case AvailableEncoding.GB2312: #if NETCORE Encoding gb2312 = CodePagesEncodingProvider.Instance.GetEncoding(936, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); //Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); //Encoding gb2312 = Encoding.GetEncoding("GB2312", new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(gb2312.GetBytes(input)); #else Encoding gb2312 = Encoding.GetEncoding(936, new EncoderReplacementFallback(strUniRepChr), new DecoderReplacementFallback(strUniRepChr)); return(gb2312.GetBytes(input)); #endif default: error.setError("AE001", "Unknown encoding"); return(null); } }