如何二进制转换成字符串?

编程入门 行业动态 更新时间:2024-10-23 18:34:24
本文介绍了如何二进制转换成字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

静态列表< 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; INT>>序列){    返回新的String(seq.Select(S = GT;(炭)s.Aggregate((A,B)=>将* 2 + b)条)ToArray的());}静态无效的主要(){   字符串s =计算器;   VAR二进制=新的List<名单,LT; INT>>();   对于(VAR计数器= 0;!计数器= s.Length;反++){       清单< INT> 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 }

更多推荐

如何二进制转换成字符串?

本文发布于:2023-11-30 11:38:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649835.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换成   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!