c# 转16进制

2021-05-18 11:30

阅读:325

标签:substr   date   ring   substring   bst   enc   bsp   encoding   converter   

1.byte[] 转换16进制字符串

   1.1 BitConverter方式

            var str = DateTime.Now.ToString();
            var encode = Encoding.UTF8;
            var bytes = encode.GetBytes(str);
            var hex = BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToLower();
            Console.WriteLine(hex);

   1.2 StringBuilder方式

           var str = DateTime.Now.ToString();
            var encode = Encoding.UTF8;
            var bytes = encode.GetBytes(str);
            StringBuilder ret = new StringBuilder();
            foreach (byte b in bytes)
            {
                //{0:X2} 大写
                ret.AppendFormat("{0:x2}", b);
            }
            var hex = ret.ToString();
            Console.WriteLine(hex);

 

2.16进制字符串转换 byte[]

          var inputByteArray = new byte[hex.Length / 2];
            for (var x = 0; x )
            {
                var i = Convert.ToInt32(hex.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            str = encode.GetString(inputByteArray);
            Console.WriteLine(str);

c# 转16进制

标签:substr   date   ring   substring   bst   enc   bsp   encoding   converter   

原文地址:https://www.cnblogs.com/YZFHKMS-X/p/11740328.html


评论


亲,登录后才可以留言!