16进制转字符-字符转16进制-有调用方法-貌似支持中文

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vipers_/article/details/81515334

        //16进制转字符-有调用方法-貌似支持中文
     private string HexStringToString(string hs, Encoding encode)
            {
                string strTemp = "";
                byte[] b = new byte[hs.Length / 2];
                for (int i = 0; i < hs.Length / 2; i++)
                {
                    strTemp = hs.Substring(i * 2, 2);
                    b[i] = Convert.ToByte(strTemp, 16);
                }
                //按照指定编码将字节数组变为字符串
                return encode.GetString(b);
         }
    //调用:
       string mu="需要转换的值";
       string jieguo== HexStringToString(mu, System.Text.Encoding.UTF8);
       Console.WriteLine("结果="+jieguo);
        
     /*字符转16进制
            char[] hexDigits  =  { '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',  '8',  '9',  'A',  'B',  'C',  'D',  'E',  'F'};
            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);  
            }
            */

猜你喜欢

转载自blog.csdn.net/vipers_/article/details/81515334
今日推荐