2015-07-20 187 views

回答

0

請檢查下面2方法。它可能對你有用。

我們可以做到這一點的字符串

String binary = "11010010011101010011"; 
int decimal = Integer.parseInt(binary,2); 
System.out.println("Decimal==>" + decimal); 

賦予二進制值,否則我們可以使用包裝類將其轉換

Integer decimalInt=Integer.valueOf("11010010011101010011",2); 
System.out.println("Decimal==>" + decimalInt.toString()); 
0
long n = Long.parseLong("11010010011101010011", 2); 

應該工作。因爲這些是32位,並且實際上負int的表示不使用parseInt左右。

還要確保沒有垃圾。 UTF-8文本文件可能以BOM char,零寬度空間開始,不可見。刪除BOM可以這樣完成:

s = s.replace("\uFEFF", ""); 
0

P.S.我的英語不好,但我會試着解釋一下。

  1. 首先,您必須反轉字符串。
  2. 然後,從一開始到結束,獲取元素並乘以2^place

下面是代碼:

public static void convertToDecimal(String binaryNumber) 
{ 
    String reverse = new StringBuffer(binaryNumber).reverse().toString(); 
    long decimal=0; 
    for(int i=0;i<reverse.length();i++){ 
    char c = reverse.charAt(i); 
    int k=c-'0'; 
    decimal=decimal+k*(long)Math.pow(2, i); 
    } 
    System.out.println(decimal); 
} 
相關問題