How to converting a string of Binary numbers into a string of Decimal numbers?

浪子不回头ぞ 提交于 2020-05-29 10:26:45

问题


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

Thank for the help.


回答1:


Its as easy as Convert.ToInt32

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

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

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

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))

Output

8,5,12,12,15

Full Demo Here


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

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
----------------------------------------------------------------------------

Test 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 % |
-------------------------------------------------------------------------------



回答2:


To convert a single like this:

Convert.ToInt32("00001000", 2)

I think we can use Regex to replace all binary numbers:

Regex.Replace(binaryNumbers, @"\b\d+\b", new MatchEvaluator((m) => { 
    return Convert.ToInt32(m.Value, 2).ToString(); 
}));


来源:https://stackoverflow.com/questions/51624882/how-to-converting-a-string-of-binary-numbers-into-a-string-of-decimal-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!