2013-05-17 31 views
0

有什麼辦法將非常大的二進制,十進制和十六進制數字轉換爲對方? 我必須用它來模擬最多256位的尋址過程。.NET將非常大的數字轉換爲不同的數字基數

我想要做下列轉換(如果有可能,它們存儲在一個對象)

非常大的二進制數 - >非常大的十進制數

非常大的二進制數 - >很大十六進制數

非常大的十進制數 - >非常大的二進制數

非常大的十進制數 - >非常大的十六進制數

非常大的十六進制數 - >非常大的二進制數

非常大的十六進制數 - >非常大的十進制數

非常大的二進制數 - >字符串

非常大的十進制數 - >字符串

非常大的十六進制數 - >字符串

分裂和連接非常大的二進制數的可能性非常重要。 如果可能的話,我會使用類支持的解決方案,並避免使用byte []類型從一個數字庫到另一個數字庫的手動轉換。

我試過BigInteger類,它可以存儲非常大的數字,但不能將它們轉換爲另一個數字庫。

+1

BigInteger.ToString可以轉換爲十六進制...但我不知道它可以做二進制。 –

+0

http://stackoverflow.com/questions/279038/working-with-incredibly-large-numbers-in-net –

+0

http://stackoverflow.com/questions/14048476/biginteger-to-hex-decimal-octal-binary -strings –

回答

0

解決方案通過Andrew Jonkers

//Convert number in string representation from base:from to base:to. 
//Return result as a string 
public static String Convert(int from, int to, String s) 
{ 
    //Return error if input is empty 
    if (String.IsNullOrEmpty(s)) 
    { 
     return ("Error: Nothing in Input String"); 
    } 
    //only allow uppercase input characters in string 
    s = s.ToUpper(); 

    //only do base 2 to base 36 (digit represented by characters 0-Z)" 
    if (from < 2 || from > 36 || to < 2 || to > 36) 
    { return ("Base requested outside range"); } 

    //convert string to an array of integer digits representing number in base:from 
    int il = s.Length; 
    int[] fs = new int[il]; 
    int k = 0; 
    for (int i = s.Length - 1; i >= 0; i--) 
    { 
     if (s[i] >= '0' && s[i] <= '9') { fs[k++] = (int)(s[i] - '0'); } 
     else 
     { 
      if (s[i] >= 'A' && s[i] <= 'Z') { fs[k++] = 10 + (int)(s[i] - 'A'); } 
      else 
      { return ("Error: Input string must only contain any of 0-9 or A-Z"); } //only allow 0-9 A-Z characters 
     } 
    } 

    //check the input for digits that exceed the allowable for base:from 
    foreach(int i in fs) 
    { 
     if (i >= from) { return ("Error: Not a valid number for this input base"); } 
    } 

    //find how many digits the output needs 
    int ol = il * (from/to+1); 
    int[] ts = new int[ol+10]; //assign accumulation array 
    int[] cums = new int[ol+10]; //assign the result array 
    ts[0] = 1; //initialize array with number 1 

    //evaluate the output 
    for (int i = 0; i < il; i++) //for each input digit 
    { 
     for (int j = 0; j < ol; j++) //add the input digit 
      // times (base:to from^i) to the output cumulator 
     { 
      cums[j] += ts[j] * fs[i]; 
      int temp = cums[j]; 
      int rem = 0; 
      int ip = j; 
      do // fix up any remainders in base:to 
      { 
       rem = temp/to; 
       cums[ip] = temp-rem*to; ip++; 
       cums[ip] += rem; 
       temp = cums[ip]; 
      } 
      while (temp >=to); 
     } 

     //calculate the next power from^i) in base:to format 
     for (int j = 0; j < ol; j++) 
     { 
      ts[j] = ts[j] * from; 
     } 
     for(int j=0;j<ol;j++) //check for any remainders 
     { 
      int temp = ts[j]; 
      int rem = 0; 
      int ip = j; 
      do //fix up any remainders 
      { 
       rem = temp/to; 
       ts[ip] = temp - rem * to; ip++; 
       ts[ip] += rem; 
       temp = ts[ip]; 
      } 
      while (temp >= to); 
     } 
    } 

    //convert the output to string format (digits 0,to-1 converted to 0-Z characters) 
    String sout = String.Empty; //initialize output string 
    bool first = false; //leading zero flag 
    for (int i = ol ; i >= 0; i--) 
    { 
     if (cums[i] != 0) { first = true; } 
     if (!first) { continue; } 
     if (cums[i] < 10) { sout += (char)(cums[i] + '0'); } 
     else { sout += (char)(cums[i] + 'A'-10); } 
    } 
    if (String.IsNullOrEmpty(sout)) { return "0"; } //input was zero, return 0 
    //return the converted string 
    return sout; 
} 
0

我有同樣的問題一次。我寫了一個簡單的方法,它將十進制數組更改爲二進制數組。

private int[] ConvertDecimalCharArrayToBinaryCharArray(int[] decimalValues) 
    { 
     List<int> result = new List<int>(); 
     bool end = false; 
     while (end == false) 
     { 
      result.Add(decimalValues[decimalValues.Length - 1] % 2); 

      int previous = 0; 
      bool allzeros = true; 
      for (int i = 0; i < decimalValues.Length; i++) 
      { 

       var x = decimalValues[i]; 
       if (x != 0) 
       { 
        allzeros = false; 
       } 

       if (allzeros && i == decimalValues.Length - 1) 
       { 
        end = true; 
       } 
       var a = (x + previous)/2; 
       if ((x + previous) % 2== 1) 
       { 
        previous = 10; 
       } 
       else 
       { 
        previous = 0; 
       } 
       decimalValues[i] = a; 
      } 
     } 

     result.RemoveAt(result.Count - 1); 
     result.Reverse(); 
     return result.ToArray(); 
    } 
相關問題