如何将一串二进制数字转换为十进制数字字符串? [英] How to converting a string of Binary numbers into a string of Decimal numbers?

查看:208
本文介绍了如何将一串二进制数字转换为十进制数字字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个采用二进制字符串并将其转换为十进制字符串的程序. 我知道你是怎么做到的:

I am wanting to make a program that take a binary string and turns it into a decimal string. I know you how do this:

Convert.ToString(00001000, 2);

要将单个二进制数字转换为单个十进制数字,但是我如何在字符串中获取一堆二进制数字,像这样:

To convert a single binary number to a single decimal number, but how would I take a bunch of binary numbers in a string, like this:

00001000 00000101 00001100 00001100 00001111
00010111 00001111 00010010 00001100 00000100

转换为十进制数字字符串,如下所示:

into a string of decimal numbers, like this:

8 5 12 12 15
23 15 18 12 4

感谢您的帮助.

推荐答案

就像

将指定基数中的数字的字符串表示形式转换为 等效的32位带符号整数.

Converts the string representation of a number in a specified base to an equivalent 32-bit signed integer.

int output = Convert.ToInt32(input, 2);

如果它在用空格隔开的列表中,只需使用 string.Split

If its in a list separated by space, just use string.Split

返回一个字符串数组,该数组包含此实例中的子字符串 由指定的字符串或Unicode元素分隔的 字符数组.

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified string or Unicode character array.

var input = "00001000 00000101 00001100 00001100 00001111";

var results = input.Split(' ')
                   .Select(x => Convert.ToInt32(x, 2))
                   .ToList();

Console.WriteLine(string.Join(",",results))

输出

8,5,12,12,15

此处完整演示

对于那些在家里玩的人来说,这是将二进制字符串转换为int完全不考虑尾音的荒谬方式

And for those of you playing at home, here is a nonsensical way to convert a string of binary to int that totally disregards endienness

注意:scale只是预期的项目数,可以轻松地用List.Add()替换它,而且成本低廉

Note : scale is just the expected number of items, it can easily be replaced with a List.Add() with little cost

[Test("BitShift", "", false)]
public unsafe List<int> Convert(string input, int scale)
{

   var list = new int[scale];

   fixed (int* plist = list)
   fixed (char* pInput = input)
   {
      var pLen = pInput + input.Length;
      var val = 0;
      var pl = plist;
      for (var p = pInput; p < pLen; p++)
         if (*p != ' ')
            val = (val << 1) + (*p - 48);
         else
         {
            *pl = val;
            pl++;
            val = 0;
         }
      *pl = val;
   }

   return list.ToList();
}

基准

以下是您享受娱乐的一些基准

Benchmarks

Here are some benchmarks for your enjoyment

----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-3770K CPU @ 3.50GHz
Description      : Intel64 Family 6 Model 58 Stepping 9
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3901 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

测试1

--- Standard input ------------------------------------------------------------
| Value    |    Average |    Fastest |    Cycles | Garbage | Test |      Gain |
--- Scale 10 --------------------------------------------------- Time 0.654 ---
| BitShift |   2.433 µs |   2.045 µs |  12.379 K | 0.000 B | Pass |    0.00 % |
| Convert  |   7.348 µs |   6.137 µs |  29.792 K | 0.000 B | Pass | -202.08 % |
--- Scale 100 -------------------------------------------------- Time 0.618 ---
| BitShift |   6.629 µs |   6.137 µs |  26.656 K | 0.000 B | Pass |    0.00 % |
| Convert  |  37.805 µs |  33.320 µs | 136.246 K | 0.000 B | Pass | -470.31 % |
--- Scale 1,000 ------------------------------------------------ Time 0.978 ---
| BitShift |  56.409 µs |  48.519 µs | 201.670 K | 0.000 B | Pass |    0.00 % |
| Convert  | 335.717 µs | 286.730 µs |   1.181 M | 0.000 B | Pass | -495.15 % |
--- Scale 10,000 ----------------------------------------------- Time 3.723 ---
| BitShift | 488.767 µs | 415.919 µs |   1.715 M | 0.000 B | Pass |    0.00 % |
| Convert  |   3.209 ms |   2.720 ms |  10.955 M | 0.000 B | Pass | -556.46 % |
--- Scale 100,000 --------------------------------------------- Time 39.832 ---
| BitShift |   5.110 ms |   4.195 ms |  17.788 M | 0.000 B | Pass |    0.00 % |
| Convert  |  42.987 ms |  35.171 ms | 141.218 M | 0.000 B | Pass | -741.28 % |
-------------------------------------------------------------------------------

这篇关于如何将一串二进制数字转换为十进制数字字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆