Exemple #1
0
        private string StrConvert(string str, string charset, StrConvertType convertType)
        {
            if (str.IsNullOrEmpty())
            {
                return("");
            }

            Encoding encoding;

            if (charset.IsNullOrEmpty() || charset == EndeCharset.GB2312)
            {
                encoding = Encoding.Default;
            }
            else if (charset == EndeCharset.UTF8)
            {
                encoding = Encoding.UTF8;
            }
            else if (charset == EndeCharset.Unicode)
            {
                encoding = Encoding.Unicode;
            }
            else
            {
                encoding = Encoding.Default;
            }

            try
            {
                switch (convertType)
                {
                case StrConvertType.StrToHex:
                    return(ConvertHelper.StrToHex(str, encoding));

                case StrConvertType.HexToStr:
                    return(ConvertHelper.HexToStr(str, encoding));

                case StrConvertType.StrToBytes:
                    return(ConvertHelper.StrToBytes(str, encoding));

                case StrConvertType.BytesToStr:
                    return(ConvertHelper.BytesToStr(str, encoding));

                default:
                    return(string.Empty);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }