2012-01-07 197 views
0

我有一個大的文本,其中包含一個數字作爲二進制值。例如'123'將是'001100010011001000110011'。編輯:應該是1111011C#將大的二進制字符串轉換爲十進制系統

現在我想將其轉換爲十進制系統,但數字太大,int64。

所以,我想要:將一個大的二進制字符串轉換爲十進制字符串。

+0

我的計算器說,1100010011001000110011₂等於3224115₁₀,不123₁₀。對於Int64,3224115√10不是太大。 – dtb 2012-01-07 23:50:42

+1

你是用Google搜索嗎?我發現這個:http://cboard.cprogramming.com/csharp-programming/123317-convert-binary-decimal-string.html – fury 2012-01-07 23:52:25

+1

你是如何得到從十進制到十進制001100010011001000110011二進制? – 2012-01-07 23:52:30

回答

8

這將這樣的伎倆:

public string BinToDec(string value) 
{ 
    // BigInteger can be found in the System.Numerics dll 
    BigInteger res = 0; 

    // I'm totally skipping error handling here 
    foreach(char c in value) 
    { 
     res <<= 1; 
     res += c == '1' ? 1 : 0; 
    } 

    return res.ToString(); 
} 
+1

OP表示即使是int64也是如此,所以他想要一個十進制字符串。 – fury 2012-01-07 23:59:42

+0

是的,改變了他的需求。 – Nuffin 2012-01-08 00:31:28

+0

BigInteger在System.Numerics中,默認情況下未引用。 – 2012-01-08 00:36:53

相關問題