2017-04-05 135 views
0

我创建一个应用程序,它会做this video - The Everything Formula将字符串转换二进制为10进制

显示我建议你看它理解这个公式。我试图复制视频的一部分,然后获取'k'(y坐标)。我将图像的每个像素都放入一个包含二进制版本的字符串中。二进制数的长度非常大,我不能将它存储为int或long。

现在,这里是我无法解决的部分。

我该如何将包含二进制数字的字符串转换为字符串格式的基本10数字?

不能使用long或int类型,它们不够大。任何使用int类型的转换都不起作用。

示例代码:

public void GraphUpdate() 
    { 
     string binaryVersion = string.Empty; 

     for (int i = 0; i < 106; i++) 
     { 
      for (int m = 0; m < 17; m++) 
      { 
       PixelState p = Map[i, m]; // Map is a 2D array of PixelState, representing the grid/graph. 

       if (p == PixelState.Filled) 
       { 
        binaryVersion += "1"; 
       } 
       else 
       { 
        binaryVersion += "0"; 
       } 
      } 
     } 

     // Convert binaryVersion to base 10 without using int or long 
    } 

public enum PixelState 
{ 
    Zero, 
    Filled 
} 
+1

“我建议你看它理解这个” --- :-D – zerkms

+0

“我将如何转换包含二进制数字符串转换成十进制数也是字符串格式? “如果你给我们一个你想要转换的字符串的例子,而不是强制我们编译和调试一个示例代码,那更好。 – ElektroStudios

+0

在它的核心,'String'只是一个'Bytes'的数组 - 你可以返回一个数组吗? –

回答

1

可以使用的BigInteger类,这是的.NET 4.0部分。 请参阅MSDN BigInteger Constructor,它将输入byte []。 这个字节[]是你的二进制数。
可以通过调用BigInteger.ToString()来检索结果字符串

+0

从二进制字符串到字节[]的转换可以在[这里]找到(http://stackoverflow.com/questions/3436398/convert-a-binary-string-representation-to-a-byte-array) – sulo

0

尝试使用Int64。这对于高至9,223,372,036,854,775,807:

using System; 

namespace StackOverflow_LargeBinStrToDeciStr 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Int64 n = Int64.MaxValue; 
      Console.WriteLine($"n = {n}"); // 9223372036854775807 

      string binStr = Convert.ToString(n, 2); 
      Console.WriteLine($"n as binary string = {binStr}"); // 111111111111111111111111111111111111111111111111111111111111111 

      Int64 x = Convert.ToInt64(binStr, 2); 
      Console.WriteLine($"x = {x}"); // 9223372036854775807 

      Console.ReadKey(); 
     } 
    } 
} 
相关问题