Binary to Text (ASCII) Conversion
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[])
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的更多相关文章
- Linux学习笔记:ftp中binary二进制与ascii传输模式的区别
在使用ftp传输文件时,常添加上一句: binary -- 使用二进制模式传输文件 遂查资料,如下所获. FTP可用多种格式传输文件,通常由系统决定,大多数Linux/UNIX系统只有两种模式:文本 ...
- Ascii vs. Binary Files
Ascii vs. Binary Files Introduction Most people classify files in two categories: binary files and A ...
- Text and Binary modes
http://perlmaven.com/what-is-a-text-file https://cygwin.com/cygwin-ug-net/using-textbinary.html Text ...
- Convert HTML to Text(转载)
原文地址:http://www.blackbeltcoder.com/Articles/strings/convert-html-to-text Download Source Code Intro ...
- javascript ASCII和Hex互转
<script> var symbols = " !\"#$%&'()*+,-./0123456789:;<=>?@"; var loAZ ...
- C# Winform 支持Hex与ASCII输入和切换的文本框
最近一直在做一个支持串口,TCP,UDP通讯调试的一体化工具(也就是C#串口调试工具 v2.0的第三版),其中涉及到16进制数据和ASCII码的输入,所以继承了TextBox的基础上,写了这个支持He ...
- ASCII 码对应表
Macron symbol ASCII CODE 238 : HTML entity : [ Home ][ español ] What is my IP address ? your public ...
- C#中ASCII码学习心得
1.利用调用ASCIIEncoding类来实现各种转换.如简单个ACS码和int转换. ***利用(int)ASCIIEncoding类对象.GetBytes(character)[0]得到整数: p ...
- C# ASCII与字符串间相互转换 (转)
引言: 最近开始学习C#,在写串口助手小工具时遇到十六进制发送与字符发送之间转换的问题, 小弟通过网络各路大神的帮助下,终于实现正确显示收发,小弟菜鸟一枚,不足之处还望各位批评指正O(∩_∩)O! 其 ...
随机推荐
- 用dubbo+zookeeper+spring搭建一个简单的http接口程序
dubbo是一个分布式服务框架,是阿里巴巴开发的一个解决RPC远程调用优化的核心框架,包含负载均衡算法,能提高分布式系统的性能. zookeeper是hadoop的一个子项目,主要用来解决分布式系统的 ...
- Warning: Attempt to present * on * which is already presenting *
Warning: Attempt to present (要被presented的控制器) on (哪个控制器来presenting) which is already presenting (已经 ...
- 03链栈_LinkStack--(栈与队列)
#include "stdio.h" #include "stdlib.h" #include "io.h" #include " ...
- 生产者消费者问题c语言实现
#include <stdio.h> #include <process.h> #include <Windows.h> //信号量与关键段 CRITICAL_SE ...
- OpenJudge/Poj 1191 棋盘分割
1.链接地址: http://bailian.openjudge.cn/practice/1191/ http://poj.org/problem?id=1191 2.题目: 总时间限制: 1000m ...
- html常用单词和各种少见标签
常用单词: 空格 align="left"valign="top"align="center"valign="middle&qu ...
- Newtonsoft.Json随手记
private static Newtonsoft.Json.JsonSerializerSettings CreateSettings(string dateFormat) { Newtonsoft ...
- 各种OS间文件传输
搞了几天才会这个法子,羞愧难当. Ubuntu安装iptux,windows下是飞鸽传输.同局域网下可以聊天,传送文件或文件夹.文件夹速度大概10M/S. 其他共享方法: ftp服务器,不成功 sam ...
- 设置nginx禁止通过IP访问服务器的方法
在Nginx上设置禁止通过IP访问服务器,只允许通过域名访问,以避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网. nginx的默认虚拟主机允许用户通过IP访问,或者通过未设置的域名访问 ...
- 怎么预防sql注入攻击
假设sql是搜索用户A的文章,sql会是这样: select * from table where owner='A'; sql注入攻击者会修改用户名来实现攻击,例如把A 改成A' or 1='1 组 ...