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. DOM4J方式解析XML文件

    dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory dom4j是一个简单的开源库,用于处理XML. X ...

  2. hanoi双塔

    汉诺塔,经典的递归. 经典的汉诺塔游戏相信很多同学都会玩的,规则就不用赘述,百科一下就OK.有三个柱子A,B,C,A柱子上套有n个大小不等的盘子,任意两个盘子,上面的盘子一定小于下面的盘子.现在请你编 ...

  3. 模板:函数memset

    需要的头文件 <memory.h> or <string.h> memset   函数介绍 void *memset(void *s, int ch, size_t n); 函 ...

  4. nginx 默认会把header里的参数去掉下划线

    做token验证的时候遇到问题:在本地可以获取前端header传的参数,但是部署到服务器获取的就是null(服务器地址用nginx做了代理) 原因: nginx代理默认会把header的参数的 &qu ...

  5. jquery与后台相互传递中文参数乱码

    前端.后台传递中文参数,乱码的情况: var a="参数乱码"; //编译两次        window.location.href=encodeURI(encodeURI(&q ...

  6. linux下面的查找

    locate:     速度快     不是实时的,每天定时执行把结果导入数据库     模糊匹配     updatedb  --手动生成文件数据库,执行时间较长   find:     实时查找 ...

  7. 12个有趣的C语言问答(详解)

    本文参照博文<12个有趣的C语言问答>,在原文的基础上增加来对应的知识点的详细介绍. 1 gets()方法 Q:下面的代码有一个被隐藏的问题,你能找到它吗? #include <st ...

  8. 最大类间方差法(Otsu)

    由Otsu(大津展之)于1978年提出的最大类间方差法,是引起较多关注的一种阈值选取方法.它是在判决分析或最小二乘原理的基础上推导出来的. 参考文献: [1] Otsu N. A threshold ...

  9. css3太极图效果+自动旋转

    主要使用border-radius属性实现圆,半圆,定位坐标覆盖部分模块. 半圆: width: 50%; height: 100%; border-radius:100% 0 0 100% /50% ...

  10. oracle,wamp,FZ突然出现问题,求解决方案(未解决,最终系统还原)

    -----背景------- 系统:win7  64位oracle 11g(11.1)每天都用oracle.用toad for oracle .电脑固定IP.未更改任何配置信息.用了几个月,突然出现了 ...