byte[]与string相互转换

byte[]转换为字符串存储,然后再转回来

            byte[] bytes = System.Text.Encoding.Default.GetBytes("fadfasfasdf");
            string str = BitConverter.ToString(bytes);

            string s2 = BitConverter.ToString(bytes);   // 82-C8-EA-17
            String[] tempAry = s2.Split('-');
            byte[] decBytes2 = new byte[tempAry.Length];
            for (int i = 0; i < tempAry.Length; i++)
                decBytes2[i] = Convert.ToByte(tempAry[i], 16);
            // decBytes2 same as bytes

            string s3 = Convert.ToBase64String(bytes);  // gsjqFw==
            byte[] decByte3 = Convert.FromBase64String(s3);


你可能感兴趣的:(WinForm)