Binary to Text (ASCII) Conversion
to Text Binary ascii CONVERSION
2023-09-11 14:14:22 时间
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 / 8; string tmp = string.Empty; List<char> list = new List<char>(); for (int i = 0; i < count; i++) { tmp = binary.Substring(i * 8, 8); list.Add((char)(Convert.ToInt32(tmp,2))); } 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(8).Select(s => Convert.ToChar(Convert.ToInt32(s, 2)))); } static IEnumerable<string> Split(this string s, int size) { for (int i = 0; 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 = 0; i < binary.Length; i += 8) byteList.Add(Convert.ToByte(binary.Substring(i, 8), 2)); 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(0, binary.Length / 8) .Select(i => binary.Substring(i * 8, 8)) .Select(s => Convert.ToByte(s, 2)).ToArray()); } }
相关文章
- json 字符串包含数组转换为object对象是报异常java.lang.ClassCastException: net.sf.ezmorph.bean.MorphDynaBean cannot be cast to
- [Typescript Challenges] 3. Easy - Tuple to Object
- [Javascript] Wait for Multiple JavaScript Promises to Settle with Promise.allSettled()
- [Algorithm] Given the root to a binary tree, return the deepest node
- [Recompose] Create Stream Behaviors to Push Props in React Components with mapPropsStream
- [Angular 2] Using Pipes to Filter Data
- 2. Using 'dp' instead of 'px' to set text size
- SAP ABAP 解析 excel 文件的函数 TEXT_CONVERT_XLS_TO_SAP 单步执行分析
- update empty content to text instance - where is B mode changed to D by frame
- How to enable multiple text type for Product
- CRM How to configure text type to allow html editor
- Text store debug FM CRM_TEXT_MAINTAIN_OW
- 在vue里运行报错:Failed to resolve loader: sass-loader You may need to install it.提示需要安装sass模块
- You currently don‘t have access to this membership resource. To resolve this issue, agree to the lat
- 成功解决To fix this you could try to: 1. loosen the range of package versions you‘ve specified
- 已解决selenium.common.exceptions.WebDriverException: Messag: ‘geckodriver‘ executable needs to be in PA
- PAT 1033. To Fill or Not to Fill (贪婪)
- 【问题解决】[kubelet-check] The HTTP call equal to ‘curl -sSL http://localhost:10248/healthz‘ failed wite
- PAT 1033. To Fill or Not to Fill (贪心)