gpt4 book ai didi

c# - 如何将一串二进制数转换为一串十进制数?

转载 作者:行者123 更新时间:2023-12-03 19:30:07 24 4
gpt4 key购买 nike

我想制作一个程序,将二进制字符串转换为十进制字符串。我知道你怎么做:

Convert.ToString(00001000, 2);

要将单个二进制数转换为单个十进制数,但我如何在字符串中获取一堆二进制数,如下所示:

00001000 00000101 00001100 00001100 00001111
00010111 00001111 00010010 00001100 00000100

转换成一串十进制数字,如下所示:

8 5 12 12 15
23 15 18 12 4

最佳答案

就像 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);

如果它位于以空格分隔的列表中,则只需使用 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))

输出

8,5,12,12,15

<强> Full Demo Here


对于那些在家玩的人来说,这是一种将二进制字符串转换为 int 的无意义方法,完全不考虑字节顺序

注意:规模只是预期的项目数量,它可以轻松地用 List.Add() 替换,成本很低

[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();
}

基准

这里有一些供您享受的基准

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

关于c# - 如何将一串二进制数转换为一串十进制数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51624882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com