Binary to Text (ASCII) Conversion

Description:

Write a function that takes in a binary string and returns the equivalent decoded text (the text is ASCII encoded).

Each 8 bits on the binary string represent 1 character on the ASCII table.

Note: In the case of an empty binary string your function should return an empty string.

using System;
using System.Collections.Generic;
using System.Linq; public static class Kata
{
public static string BinaryToString(string binary)
{
int count = binary.Length / ;
string tmp = string.Empty;
List<char> list = new List<char>();
for (int i = ; i < count; i++)
{
tmp = binary.Substring(i * , );
list.Add((char)(Convert.ToInt32(tmp,)));
}
return string.Join(string.Empty,list);
}
}

其他人的解法:

binary.Split(8)的用法,通过this关键字对string类进行了扩展,增加了扩展方法Split(int n)
IEnumerable<string>和yield的用法

Convert.ToChar的用法

using System;
using System.Linq;
using System.Collections.Generic; public static class Kata
{
public static string BinaryToString(string binary)
{
return string.Join("", binary.Split().Select(s => Convert.ToChar(Convert.ToInt32(s, ))));
} static IEnumerable<string> Split(this string s, int size)
{
for (int i = ; i < s.Length; i += size) {
yield return s.Substring(i, Math.Min(size, s.Length - i));
}
}
}

Encoding.GetString 方法 (Byte[])

ASCIIEncoding

using System;
using System.Text;
using System.Collections.Generic; public static class Kata
{
public static string BinaryToString(string binary)
{
List<Byte> byteList = new List<Byte>();
for (int i = ; i < binary.Length; i += )
byteList.Add(Convert.ToByte(binary.Substring(i, ), ));
return Encoding.ASCII.GetString(byteList.ToArray());
}
}

上面版本的装逼写法

using System;
using System.Text;
using System.Linq; public static class Kata
{
public static string BinaryToString(string binary)
{
return Encoding.ASCII.GetString(Enumerable.Range(, binary.Length / )
.Select(i => binary.Substring(i * , ))
.Select(s => Convert.ToByte(s, )).ToArray());
}
}

Binary to Text (ASCII) Conversion的更多相关文章

  1. Linux学习笔记:ftp中binary二进制与ascii传输模式的区别

    在使用ftp传输文件时,常添加上一句: binary  -- 使用二进制模式传输文件 遂查资料,如下所获. FTP可用多种格式传输文件,通常由系统决定,大多数Linux/UNIX系统只有两种模式:文本 ...

  2. Ascii vs. Binary Files

    Ascii vs. Binary Files Introduction Most people classify files in two categories: binary files and A ...

  3. Text and Binary modes

    http://perlmaven.com/what-is-a-text-file https://cygwin.com/cygwin-ug-net/using-textbinary.html Text ...

  4. Convert HTML to Text(转载)

    原文地址:http://www.blackbeltcoder.com/Articles/strings/convert-html-to-text  Download Source Code Intro ...

  5. javascript ASCII和Hex互转

    <script> var symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@"; var loAZ ...

  6. C# Winform 支持Hex与ASCII输入和切换的文本框

    最近一直在做一个支持串口,TCP,UDP通讯调试的一体化工具(也就是C#串口调试工具 v2.0的第三版),其中涉及到16进制数据和ASCII码的输入,所以继承了TextBox的基础上,写了这个支持He ...

  7. ASCII 码对应表

    Macron symbol ASCII CODE 238 : HTML entity : [ Home ][ español ] What is my IP address ? your public ...

  8. C#中ASCII码学习心得

    1.利用调用ASCIIEncoding类来实现各种转换.如简单个ACS码和int转换. ***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数: p ...

  9. C# ASCII与字符串间相互转换 (转)

    引言: 最近开始学习C#,在写串口助手小工具时遇到十六进制发送与字符发送之间转换的问题, 小弟通过网络各路大神的帮助下,终于实现正确显示收发,小弟菜鸟一枚,不足之处还望各位批评指正O(∩_∩)O! 其 ...

随机推荐

  1. ###《More Effective C++》- 异常

    More Effective C++ #@author: gr #@date: 2015-05-24 #@email: forgerui@gmail.com 九.利用destructors避免泄漏资源 ...

  2. 【html】【8】div布局[子div在父div居底]

    从今天起 开始细话div布局   思路及要点: 父div的位置设置成相对的,即“position: relative;”. 而子div的位置设置成绝对的,并且下边缘设为0,即“position: ab ...

  3. C# 匿名类型

    c#3.0引入匿名类型,其由编译器动态生成而非显式定义. using System;using System.Collections.Generic;using System.Linq;using S ...

  4. [译]CSS content

    原文地址:http://css-tricks.com/css-content/ CSS中有一个属性content,只能和伪元素:before和:after一起使用,他们的写法像伪类选择器(前面有冒号) ...

  5. 如何在Markdown、HTML编辑器上输入一些特殊字符

    如何输入EntityCode 参考: 1.EntityCode 2.Common HTML entities used for typography 3.Latin Supplement-拉丁补充

  6. CentOS7 安装LAMP环境

    1.使用yum安装 yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-pos ...

  7. H5发展简介

    HTML4.01 超文本标记语言,1999年12月24日由W3C组织发布. XHTML 扩展的超文本标记语言(eXtensible Hyper Text Markup Language),和HTML4 ...

  8. Android开发中activity切换动画的实现

    (1)我们在MainAcitvity中定义两个textview,用于点击触发切换Activity事件,下面是布局文件代码. <LinearLayout android:layout_width= ...

  9. 【1】Bootstrap入门引言

    Bootstrap学习者要具备的一些要求: [1]xhtml常用标签的基础知识 [2]xhtml+css布局的基础知识 [3]html5+css3的基础知识 ===================== ...

  10. Linux启用MySQL的InnoDB引擎

    前几天公司的一个项目组的同事反应说公司内部的一台Linux服务器上的MySQL没有InnoDB这个引擎,我当时想应该不可能啊,MySQL默认应该 就已经安装了这个引擎的吧,于是上服务器去看了看,发现还 ...