c# Encoding.Unicode.GetBytes 转码取字符串

  private string GetUnicodeString(byte[] buffer)
        {
            int size = 0;
            if (buffer != null && (size = buffer.Length) >= 2)
            {
                size -= size % 2; // sizeof(wchar_t)
                char[] value = new char[size / 2];
                for (int i = 0; i < size; i += 2)
                    value[i / 2] = (char)((buffer[i + 1] << 8) + buffer[i]);
                return new string(value);
            }
            return string.Empty;
        }
        public IActionResult Index()
        {

            

            List<byte> list = new List<byte> { 0, 8, 0, 0, 6, 1, 0, 0, 0, 8 };
            byte[] bytes = Encoding.Unicode.GetBytes("MSDTC");
            list.AddRange(BitConverter.GetBytes(bytes.Length));
            list.AddRange(bytes);
           
            byte[] rs = list.ToArray();
            //"000800000601000000080A0000004D005300440054004300"

            List<byte> nlist = new List<byte>();
            nlist.AddRange(rs);
            for(int i = 13; i>=0;i--)
            {
                nlist.RemoveAt(i);
            }
            rs = nlist.ToArray();
            var num = rs.Count();

            var hex = Convert.ToHexString(bytes);
            var hex1 = Convert.ToHexString(rs);
            
            var abc = Encoding.UTF8.GetString(bytes);
            var abc2 = Encoding.UTF8.GetString(rs);
            var ddd = abc;
            //"4D005300440054004300"
             
            hex = GetUnicodeString(bytes);
          var   hex2= GetUnicodeString(rs);
            string aa = hex;
            string bb = aa;
            return View();
        }

posted on 2022-06-07 16:34 青蛙學堂 阅读(792) 评论(1)  编辑 收藏 引用 所属分类: c#

评论

# re: c# Encoding.Unicode.GetBytes 转码取字符串 2022-06-08 15:08 青蛙學堂


select top 10 CONVERT(nvarchar(max),substring(CONVERT(varbinary(MAX), aaa ),31,len( CONVERT(varbinary(MAX), aaa ))), 0) aa
  回复  更多评论   

只有注册用户登录后才能发表评论。
<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜