回答
使用Integer.parseInt
(見javadoc),即轉換您的String
到int
使用基二:
int decimalValue = Integer.parseInt(c, 2);
int i = Integer.parseInt(c, 2);
我認爲你正在尋找Integer.parseInt。第二個參數需要一個基數,在這種情況下是2。
Integer.parseInt(c, 2)
public static int integerfrmbinary(String str){
double j=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)== '1'){
j=j+ Math.pow(2,str.length()-1-i);
}
}
return (int) j;
}
這段代碼我已經手動寫入。如上所述,您也可以使用parseInt。 此功能將給予對應的二進制串:)
int num = Integer.parseInt("binaryString",2);
public static void convertStringToDecimal(String binary)
{
int decimal=0;
int power=0;
while(binary.length()>0)
{
int temp = Integer.parseInt(binary.charAt((binary.length())-1)+"");
decimal+=temp*Math.pow(2, power++);
binary=binary.substring(0,binary.length()-1);
}
System.out.println(decimal);
}
public static Long binToDec(String bin) {
long dec = 0L;
long pow = 1L;
for (int i = (bin.length() - 1); i >= 0; i--) {
char c = bin.charAt(i);
dec = dec + (Long.parseLong(c + "") * pow);
pow = pow * 2;
}
return dec;
}
或
long num = Long.parseLong("101110111",2);
測試它
import java.util.Scanner;
public class BinaryToDecimal{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int binaryNumber = 0;
int counter = 0;
int number = 0;
System.out.print("Input binary number: ");
binaryNumber = input.nextInt();
//it's going to stop when the binaryNumber/10 is less than 0
//example:
//binaryNumber = 11/10. The result value is 1 when you do the next
//operation 1/10 . The result value is 0
while(binaryNumber != 0)
{
//Obtaining the remainder of the division and multiplying it
//with the number raised to two
//adding it up with the previous result
number += ((binaryNumber % 10)) * Math.pow(2,counter);
binaryNumber /= 10; //removing one digit from the binary number
//Increasing counter 2^0, 2^1, 2^2, 2^3.....
counter++;
}
System.out.println("Decimal number : " + number);
}
}
十進制值添加一些解釋和答案,這個答案如何幫助OP在修復當前問題 –
private static int convertBinaryToDecimal(String strOfBinary){
int flag = 1, binary=0;
char binaryOne = '1';
char[] charArray = strOfBinary.toCharArray();
for(int i=charArray.length-1;i>=0;i--){
if(charArray[i] == binaryOne){
binary+=flag;
}
flag*=2;
}
return binary;
}
public static void main(String[] args) {
java.util.Scanner scan = new java.util.Scanner(System.in);
long decimalValue = 0;
System.out.println("Please enter a positive binary number.(Only 1s and 0s)");
//This reads the input as a String and splits each symbol into
//array list
String element = scan.nextLine();
String[] array = element.split("");
//This assigns the length to integer arrys based on actual number of
//symbols entered
int[] numberSplit = new int[array.length];
int position = array.length - 1; //set beginning position to the end of array
//This turns String array into Integer array
for (int i = 0; i < array.length; i++) {
numberSplit[i] = Integer.parseInt(array[i]);
}
//This loop goes from last to first position of an array making
//calculation where power of 2 is the current loop instance number
for (int i = 0; i < array.length; i++) {
if (numberSplit[position] == 1) {
decimalValue = decimalValue + (long) Math.pow(2, i);
}
position--;
}
System.out.println(decimalValue);
main(null);
}
只是想分享我的解決方案:) – George
必須考慮小數精度,因此必須限制位串的長度。無論如何,使用BigDecimal是一個不錯的選擇。
public BigDecimal bitStringToBigDecimal(String bitStr){
BigDecimal sum = new BigDecimal("0");
BigDecimal base = new BigDecimal(2);
BigDecimal temp;
for(int i=0;i<bitStr.length();i++){
if(bitStr.charAt(i)== '1'){
int exponent= bitStr.length()-1-i;
temp=base.pow(exponent);
sum=sum.add(temp);
}
}
return sum;
}
你可以測試:str =「10000101001001000010111100100110000000000000000000001010111101」; –
import java.util.*;
public class BinaryToDecimal
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the binary number");
double s=sc.nextDouble();
int c=0;
long s1=0;
while(s>0)
{
s1=s1+(long)(Math.pow(2,c)*(long)(s%10));
s=(long)s/10;
c++;
}
System.out.println("The respective decimal number is : "+s1);
}
}
- 1. 將二進制字符串轉換爲二進制或十進制值
- 2. 功能爲二進制字符串轉換爲十進制
- 3. 如何將十六進制字符串轉換爲十進制?
- 4. Python3 ASCII十六進制轉換爲二進制字符串
- 5. Python從二進制字符串轉換爲十六進制
- 6. 轉換二進制字符串爲十六進制
- 7. 將十六進制字符串轉換爲二進制
- 8. C++長十六進制字符串轉換爲二進制
- 9. 轉換十六進制字符串爲二進制的SQL Server
- 10. 從二進制轉換爲十六進制字符串
- 11. 蟒蛇十六進制二進制轉換爲字符串
- 12. 將二進制長字符串轉換爲十六進制c#
- 13. 從二進制字符串轉換爲十進制?
- 14. 將ascii字符十進制值的字符串轉換爲二進制值
- 15. 如何字符轉換爲十六進制和二進制
- 16. 從十進制轉換爲二進制
- 17. 將二進制轉換爲十進制
- 18. 二進制轉換爲十進制 - understaing
- 19. 從二進制轉換爲十進制
- 20. 十六進制轉換爲二進制
- 21. 將十進制轉換爲二進制
- 22. 從二進制轉換爲十進制
- 23. 十六進制轉換爲二進制
- 24. 轉換該字符串爲十進制
- 25. 將字符串轉換爲十進制
- 26. 轉換十六進制字符串十六進制值
- 27. 從六進制十進制值轉換爲字符串
- 28. 將十六進制轉換爲二進制,然後轉換爲十進制
- 29. 將字符串轉換爲二進制,並在Python中將二進制轉換爲十進制?
- 30. 將十六進制字符串轉換爲二進制字符串,顯示所有4位十六進制值
請從張貼的你還沒有嘗試自己解決問題避免。 [問] – Merlin