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. Colored Linux Man pages

    Colored Linux Man pages 一.什么是Linux Man 参考: 二.如何高效率地使用Man 三.给Linux Man命令添加点颜色. 1.Unix / Linux: Displa ...

  2. 第28条:利用有限制通配符来提升API的灵活性

    参数化类型是不可变的.对两个不同类型T1和T2而言,List<T1>与List<T2>没有父子类型关系. 考虑: public class Stack<E> { p ...

  3. a标签根据js返回值判断页面是否跳转

    a标签再跳转之前先判断是否符合条件,符合可以跳转,不符合不可以跳转. 自己遇到的问题是:在js方法中根据条件就return结果,但是不行. 原因是:在js方法中return后不会结束整个js方法(ac ...

  4. CSS选择器,标签限定

    例子:ul#nav, ul li#nav和 #nav ul, #nav ul li 注意空格,没有空间隔开的就可以理解为限定 区别 1.ul#nav:表示id='nav'的ul:(ul限定#nav标签 ...

  5. Python3 IO

    在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中 ...

  6. Python3 面向对象

    Class 在Python中,定义类是通过class关键字: class Student(object): pass class后面紧接着是类名,即Student,类名通常是大写开头的单词,紧接着是( ...

  7. [C#]判断是否是合法的IP4,IP6地址

    判断一个字符串如果没有端口的话,利用IPAddress.TryParse很好判断,那么有端口怎么判断呢,正则表达式?还是其他方式? 关键代码: /// <summary> /// 判断是否 ...

  8. C# 实体model验证输出

    新建Model实体: [Required(ErrorMessage = @"地址 1 为必填项!")] [StringLength(, ErrorMessage = @" ...

  9. java异常处理机制 (转载)

    java异常处理机制 本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 异常处理是程序设计中一个非常重要的方面,也是程序设计的一大难点,从C ...

  10. oracle服务介绍

    按照windows 7 64位 安装oracle 11g R2中的方法成功安装Oracle 11g后,共有7个服务,这七个服务的含义分别为: 1. Oracle ORCL VSS Writer Ser ...