Binary to Text (ASCII) Conversion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.