首页 > Windows开发 > 详细

Convert string to binary and binary to string in C#

时间:2015-12-08 14:28:39      阅读:402      评论:0      收藏:0      [点我收藏+]

String to binary method:

 1 public static string StringToBinary(string data)
 2 {
 3     StringBuilder sb = new StringBuilder();
 4  
 5     foreach (char c in data.ToCharArray())
 6     {
 7         sb.Append(Convert.ToString(c, 2).PadLeft(8, 0));
 8     }
 9     return sb.ToString();
10 }

Binary to string method:

 1 public static string BinaryToString(string data)
 2 {
 3     List<Byte> byteList = new List<Byte>();
 4  
 5     for (int i = 0; i < data.Length; i += 8)
 6     {
 7         byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
 8     }
 9     return Encoding.ASCII.GetString(byteList.ToArray());
10 }

 

Convert string to binary and binary to string in C#

原文:http://www.cnblogs.com/frankyou/p/5028921.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!