Binary to Text (ASCII) Conversiondescription:
Write a function that takes in a binary string and returns the equivalent decoded text (the text is ASCII encoded).
Each of the 8 bits on the binary string represent 1 character on the ASCII table.
Note:in the case of a empty binary string your function should return an empty string.
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq; Public Static classkata{ Public Static stringBinaryToString (stringbinary) { intCount = binary. Length/8; stringTMP =string. Empty; List<Char> list =Newlist<Char>(); for(inti =0; I < count; i++) {tmp= Binary. Substring (i *8,8); List. ADD (Char) (Convert.ToInt32 (TMP,2))); } return string. Join (string. Empty,list); }}
Other people's Solution:
Binary. Split (8) uses the This keyword to extend the string class, adding extension methods split (int n)
The use of ienumerable<string> and yield
The use of Convert.tochar
usingSystem;usingSystem.Linq;usingSystem.Collections.Generic; Public Static classkata{ Public Static stringBinaryToString (stringbinary) { return string. Join ("", Binary. Split (8). Select (s = Convert.tochar (Convert.ToInt32 (s),2)))); } Staticienumerable<string> Split ( This stringSintsize) { for(inti =0; i < s.length; i + =size) { yield returnS.substring (i, math.min (size, s.length-i)); } }}
Encoding.getstring method (byte[])
ASCIIEncoding
usingSystem;usingSystem.Text;usingSystem.Collections.Generic; Public Static classkata{ Public Static stringBinaryToString (stringbinary) {List<Byte> bytelist =NewList<byte>(); for(inti =0; I < binary. Length; i + =8) Bytelist.add (convert.tobyte (binary). Substring (i,8),2)); returnEncoding.ASCII.GetString (Bytelist.toarray ()); }}
The above version of the installation of the forced wording
usingSystem;usingSystem.Text;usingSystem.Linq; Public Static classkata{ Public Static stringBinaryToString (stringbinary) { returnEncoding.ASCII.GetString (Enumerable.range (0, Binary. Length/8) . Select (i= = Binary. Substring (i *8,8)) . Select (S= Convert.tobyte (S,2)). ToArray ()); }}
Binary to Text (ASCII) Conversion