2012-04-16 59 views
61

我有一個代表二進制數(無前導零)字符串數組,我想轉化爲相應的基地10個號碼。考慮:如何二進制字符串轉換爲10進制在Java中

binary 1011 becomes integer 11 
binary 1001 becomes integer 9 
binary 11 becomes integer 3 etc. 

什麼是最好的處理方式?我一直在探索java.lang.number。*而沒有找到直接的轉換方法。 Integer.parseInt(b)產生一個整數等於字符串...如,1001變爲1001,而不是9 ...和似乎不包括用於輸出基底的參數。 toBinaryString轉換方向錯誤。我懷疑我需要做一個多轉換,但似乎無法找到的方法或子類的正確組合。我也不確定前導零或缺少的問題的程度。任何人有任何好的方向指向我?

+4

看[整數#parseInt函數( String s,int radix)](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Integer.html#parseInt%28java.lang.String,%20int%29) – anubhava 2012-04-16 17:49:49

+0

[將二進制數字轉換爲十進制數字](http://stackoverflow.com/qu estions/2115346 /轉換 - 二進制數字在十進制數字) – 2012-04-16 17:50:11

回答

166

你需要specify the radix。還有的Integer#parseInt()過載,它允許你。

int foo = Integer.parseInt("1001", 2); 
+1

完美。我完全錯過了允許基數的parseInt文檔的第二行。像夢一樣工作。 – dwwilson66 2012-04-16 17:58:35

+1

這是否也適用於前導零?只是確認,雖然我看不出爲什麼沒有。 – Siddhartha 2017-05-25 15:22:51

15

這可能會實現:

public int binaryToInteger(String binary) { 
    char[] numbers = binary.toCharArray(); 
    int result = 0; 
    for(int i=numbers.length - 1; i>=0; i--) 
     if(numbers[i]=='1') 
      result += Math.pow(2, (numbers.length-i - 1)); 
    return result; 
} 
+8

-1不必要的重新發明車輪。 – 2012-04-16 17:57:15

+0

我想這是沒有必要的。當你在兩節課之間有一點點時間時會發生這種情況。 – Hassan 2012-04-16 17:58:14

+5

這個對我有幫助,因爲我必須做一個轉換的學校項目,而不使用java已經有的 – bucksnort2 2013-10-16 13:46:13

2
public Integer binaryToInteger(String binary){ 
    char[] numbers = binary.toCharArray(); 
    Integer result = 0; 
    int count = 0; 
    for(int i=numbers.length-1;i>=0;i--){ 
     if(numbers[i]=='1')result+=(int)Math.pow(2, count); 
     count++; 
    } 
    return result; 
} 

我想我更無聊!修改哈桑的答案正確運作。

4
int foo = Integer.parseInt("1001", 2); 
如果你正在處理的正數,但如果你需要處理的符號數,你可能需要簽署延長您的字符串,然後轉換爲int

public class bit_fun { 
    public static void main(String[] args) { 
     int x= (int)Long.parseLong("FFFFFFFF", 16); 
     System.out.println("x =" +x);  

     System.out.println(signExtend("1")); 
     x= (int)Long.parseLong(signExtend("1"), 2); 
     System.out.println("x =" +x); 

     System.out.println(signExtend("0")); 
     x= (int)Long.parseLong(signExtend("0"), 2); 
     System.out.println("x =" +x); 

     System.out.println(signExtend("1000")); 
     x= (int)Long.parseLong(signExtend("1000"), 2); 
     System.out.println("x =" +x); 

     System.out.println(signExtend("01000")); 
     x= (int)Long.parseLong(signExtend("01000"), 2); 
     System.out.println("x =" +x); 
    } 

    private static String signExtend(String str){ 
     //TODO add bounds checking 
     int n=32-str.length(); 
     char[] sign_ext = new char[n]; 
     Arrays.fill(sign_ext, str.charAt(0)); 

     return new String(sign_ext)+str; 
    } 
} 

output: 
x =-1 
11111111111111111111111111111111 
x =-1 
00000000000000000000000000000000 
x =0 
11111111111111111111111111111000 
x =-8 
00000000000000000000000000001000 
x =8 

我希望幫助

作品就好了!

5
static int binaryToInt (String binary){ 
    char []cA = binary.toCharArray(); 
    int result = 0; 
    for (int i = cA.length-1;i>=0;i--){ 
     //111 , length = 3, i = 2, 2^(3-3) + 2^(3-2) 
     //     0   1 
     if(cA[i]=='1') result+=Math.pow(2, cA.length-i-1); 
    } 
    return result; 
} 
-1

我愛循環!好極了!

​​

While循環用蓄電池,左到右(l不改變):

int n = 0, 
    j = -1, 
    l = myString.length(); 
while (++j < l) n = (n << 1) + (myString.charAt(j) == '0' ? 0 : 1); 
return n; 

從右到左用2個循環增值經銷商,通過Convert boolean to int in Java(絕對可怕)的啓發:

int n = 0, 
    j = myString.length, 
    i = 1; 
while (j-- != 0) n -= (i = i << 1) * new Boolean(myString.charAt(j) == '0').compareTo(true); 
return n >> 1; 

稍微更合理的實施:

int n = 0, 
    j = myString.length(), 
    i = 1; 
while (j-- != 0) n += (i = i << 1) * (myString.charAt(j) == '0' ? 0 : 1); 
return n >> 1; 

可讀版本:P

int n = 0; 
for (int j = 0; j < myString.length(); j++) { 
    n *= 2; 
    n += myString.charAt(j) == '0' ? 0 : 1; 
} 
return n; 
0

Java的的Integer.parseInt(文本)的修正版與負數的工作:

public static int parseInt(String binary) { 
    if (binary.length() < Integer.SIZE) return Integer.parseInt(binary, 2); 

    int result = 0; 
    byte[] bytes = binary.getBytes(); 

    for (int i = 0; i < bytes.length; i++) { 
     if (bytes[i] == 49) { 
      result = result | (1 << (bytes.length - 1 - i)); 
     } 
    } 

    return result; 
} 
0

如果你擔心性能,Integer.parseInt()Math.pow()是太貴了。您可以使用位操作,以做同樣的事情快兩倍(根據我的經驗):

final int num = 87; 
String biStr = Integer.toBinaryString(num); 

System.out.println(" Input Number: " + num + " toBinary "+ biStr); 
int dec = binaryStringToDecimal(biStr); 
System.out.println("Output Number: " + dec + " toBinary "+Integer.toBinaryString(dec)); 

int binaryStringToDecimal(String biString){ 
    int n = biString.length();  
    int decimal = 0; 
    for (int d = 0; d < n; d++){ 
    // append a bit=0 (i.e. shift left) 
    decimal = decimal << 1; 

    // if biStr[d] is 1, flip last added bit=0 to 1 
    if (biString.charAt(d) == '1'){ 
     decimal = decimal | 1; // e.g. dec = 110 | (00)1 = 111 
    } 
    } 
    return decimal; 
} 

輸出:

Input Number: 87 toBinary 1010111 
Output Number: 87 toBinary 1010111 
相關問題