美文网首页
C# 数据操作工具类

C# 数据操作工具类

作者: Rinaloving | 来源:发表于2022-05-07 13:21 被阅读0次
将Unicode编码转换为汉字字符串
         /// 
        /// 将Unicode编码转换为汉字字符串 
        /// 
        /// Unicode编码字符串 
        /// 汉字字符串 
        public static string ToGB2312(string str)
        {
            MatchCollection mc = Regex.Matches(str, "([\\w]+)|(\\\\u([\\w]{4}))");
            if (mc != null && mc.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (Match m2 in mc)
                {
                    string v = m2.Value;
                    if (v.StartsWith("\\"))
                    {
                        string word = v.Substring(2);
                        byte[] codes = new byte[2];
                        int code = Convert.ToInt32(word.Substring(0, 2), 16);
                        int code2 = Convert.ToInt32(word.Substring(2), 16);
                        codes[0] = (byte)code2;
                        codes[1] = (byte)code;
                        sb.Append(Encoding.Unicode.GetString(codes));
                    }
                    else
                    {
                        sb.Append(v);
                    }
                }
                return sb.ToString();
            }
            else
            {
                return str;
            }
        }

2进制string 转byte[]
        /// <summary>
        /// 2进制string 转byte[]
        /// </summary>
        /// <param name="orgStr"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this string orgStr)
        {
            byte[] result = null;
            result = new byte[orgStr.Length];
            for(int i=0; i < orgStr.Length; i++)
            {
                result[i] = Convert.ToByte(orgStr.Substring(orgStr.Length-1-i, 1));
            }
            return result;
        }
16进制转2进制
       /// <summary>
        ///  16进制转2进制 
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static string HexString2BinString(string hexString)
        {
            try
            {
                string result = string.Empty;
                foreach (char c in hexString)
                {
                    int v = Convert.ToInt32(c.ToString(), 16);
                    int v2 = int.Parse(Convert.ToString(v, 2));
                    // 去掉格式串中的空格,即可去掉每个4位二进制数之间的空格,
                    result += string.Format("{0:d4} ", v2);
                }
                return result;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
字节数组转16进制字符串
         /// <summary>
        /// 字节数组转16进制字符串:空格分隔
        /// </summary>
        /// <param name="byteDatas"></param>
        /// <returns></returns>
        public static string ToHexStrFromByte(this byte[] byteDatas)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < byteDatas.Length; i++)
            {
                builder.Append(string.Format("{0:X2} ", byteDatas[i]));
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 16进制字符串转字符数组
        /// </summary>
        /// <param name="theHex"></param>
        /// <returns></returns>
        public static byte[] ToDigitsBytes(string theHex)
        {
            byte[] bytes = new byte[theHex.Length / 2 + (((theHex.Length % 2) > 0) ? 1 : 0)];
            for (int i = 0; i < bytes.Length; i++)
            {
                char lowbits = theHex[i * 2];
                char highbits;

                if ((i * 2 + 1) < theHex.Length)
                    highbits = theHex[i * 2 + 1];
                else
                    highbits = '0';

                int a = (int)GetHexBitsValue((byte)lowbits);
                int b = (int)GetHexBitsValue((byte)highbits);
                bytes[i] = (byte)((a << 4) + b);
            }

            return bytes;
        }
字符数组转16进制字符串
        /// <summary>
        /// 字符数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ToHexString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length * 2];

            for (int i = 0; i < bytes.Length; i++)
            {
                int b = bytes[i];
                chars[i * 2] = hexDigits[b >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0xF];
            }

            return new string(chars);
        }

 static char[] hexDigits = {
            '0','1','2','3','4','5','6','7',
            '8','9','A','B','C','D','E','F'};

        public static byte GetHexBitsValue(byte ch)
        {
            byte sz = 0;
            if (ch <= '9' && ch >= '0')
                sz = (byte)(ch - 0x30);
            if (ch <= 'F' && ch >= 'A')
                sz = (byte)(ch - 0x37);
            if (ch <= 'f' && ch >= 'a')
                sz = (byte)(ch - 0x57);

            return sz;
        }

字符串转Unicode
        /// <summary>
        ///  字符串转Unicode
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string String2Unicode(string source)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(source);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }
16进制转字符串
        /// <summary>
        /// 16进制转字符串 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private static string hexStrToStr(string str)
        {
            //去除字符串中的空格
            string[] strT = str.Split(' ');
            string strA = "";
            foreach (string strB in strT)
            {
                strA += strB;
            }

            char[] chars = strA.ToCharArray();
            string returnstr = "";
            string[] str1 = new string[chars.Length / 2];

            for (int i = 0; i < chars.Length / 2; i++)
            {
                string str111 = chars[2 * i].ToString() + chars[2 * i + 1].ToString();
                uint num = uint.Parse(str111, System.Globalization.NumberStyles.AllowHexSpecifier);
                char charr = (char)num;
                returnstr = returnstr + charr;
            }
            return returnstr;
        }

相关文章

网友评论

      本文标题:C# 数据操作工具类

      本文链接:https://www.haomeiwen.com/subject/inxxurtx.html