本文介绍了如何二进制转换成字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
静态列表< INT> ConvertTextToBinary(INT编号,INT基准){ 清单< INT>名单=新名单,LT; INT>(); 而(数字!= 0) { list.Add(数量%基础); 数=号/基地; } list.Reverse(); 返回列表;}静态无效的主要(字串[] args){ 字符串s =计算器; INT计数器= 0; 而(计数器!= s.Length) { INT [] A = ConvertTextToBinary(S [计数器],2).ToArray(); 的for(int i = 0; I<则为a.length;我++) { Console.Write(一个由[i]); } Console.Write(\\ n); 反++; }}
我写的字符串转换为可执行文件,它做工精细的方法。但现在我想二进制转换为字符串,如:1101000等于为h
。解决方案 静态字符串ConvertBinaryToText(列表&LT;名单,LT; INT&GT;&gt;序列){ 返回新的String(seq.Select(S = GT;(炭)s.Aggregate((A,B)=&gt;将* 2 + b)条)ToArray的());}静态无效的主要(){ 字符串s =计算器; VAR二进制=新的List&LT;名单,LT; INT&GT;&GT;(); 对于(VAR计数器= 0;!计数器= s.Length;反++){ 清单&LT; INT&GT; A = ConvertTextToBinary(S [计数器],2); binary.Add(一); 的foreach(在VAR位){ Console.Write(位); } Console.Write(\\ n); } 字符串str = ConvertBinaryToText(二进制); Console.WriteLine(STR); //计算器} static List<int> ConvertTextToBinary(int number, int Base) { List<int> list = new List<int>(); while (number!=0) { list.Add(number % Base); number = number / Base; } list.Reverse(); return list; } static void Main(string[] args) { string s = "stackoverflow"; int counter=0; while (counter!=s.Length) { int[] a = ConvertTextToBinary(s[counter], 2).ToArray(); for (int i = 0; i < a.Length; i++) { Console.Write(a[i]); } Console.Write("\n"); counter++; } }I wrote a method to convert string to binary, its working fine. But now I want to convert binary to string eg: 1101000 is equal to h.
解决方案 static string ConvertBinaryToText(List<List<int>> seq){ return new String(seq.Select(s => (char)s.Aggregate( (a,b) => a*2+b )).ToArray()); } static void Main(){ string s = "stackoverflow"; var binary = new List<List<int>>(); for(var counter=0; counter!=s.Length; counter++){ List<int> a = ConvertTextToBinary(s[counter], 2); binary.Add(a); foreach(var bit in a){ Console.Write(bit); } Console.Write("\n"); } string str = ConvertBinaryToText(binary); Console.WriteLine(str);//stackoverflow }
更多推荐
如何二进制转换成字符串?
发布评论