将二进制变为十进制
change binary to decimal
我必须编写一个将二进制转换为十进制的方法。
编写一个方法,将提供的二进制数字(作为字符串)转换为十进制数。
- convertToDecimal("01101011") = 107
- convertToDecimal("00001011") = 11
我创建它是为了将十进制更改为二进制,但是我不确定如何将它创建为二进制到十进制。
public String convertToBinary(int decimal) {
int n = decimal;
int digit;
String out = "";
while (n > 0){
n = decimal/2;
digit = decimal % 2;
out = digit + out;
decimal = n;
}
out = addPadding(out);
return out;
}
private String addPadding(String s){
String out = s;
int len = s.length();
if (len == 8) return s;
else{
switch(len){
case 7:
out = "0"+s;
break;
case 6:
out = "00"+s;
break;
case 5:
out = "000"+s;
break;
}
}
return out;
}
}
看看怎么做,这里有详细的算法:
https://javarevisited.blogspot.com/2015/01/how-to-convert-binary-number-to-decimal.html
建议的算法实现采用 int 作为输入。这是字符串版本:
public static int binaryToDecimal(String binary) {
int decimal = 0;
int power = 0;
int currentIndex = binary.length() - 1;
while (currentIndex>=0) {
int currentDigit = binary.charAt(currentIndex) - '0'; //char to number conversion
decimal += currentDigit * Math.pow(2, power);
power++;
currentIndex--;
}
return decimal;
}
需要将字符转换为数字,因为我们需要将字符“1”或“0”转换为数字 1 或 0。我们可以使用字符的 ascii 码(“0”=48和'1'=49)
1. We have to traverse from backwards as we do in the binary to decimal conversion.
2. If the character is '1', then we will add the value of power of 2.
public static int binaryToDecimal(String binary) {
int value = 0,power = 0;
for(int i = binary.length() - 1; i >= 0;i--) {
if(binary.charAt(i) == '1') {
value += Math.pow(2, power);
}
power++;
}
return value;
}
不确定您是否必须由您的老师手动完成,但如果不是,您可以使用一些可用的内置函数:
// Convert binary String to integer:
// TODO: Surround with try-catch
int outputInt = Integer.parseInt(inputBinaryString, 2);
// Convert integer to binary String:
String outputBinaryString = Integer.toBinaryString(inputInt);
// Pad leading zeros to make the length 8:
String paddedResult = String.format("%8s", outputBinaryString).replace(' ', '0');
试试这个代码
import java.util.Scanner;
class BinaryToDecimal
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a binary number:");
int n=s.nextInt();
int decimal=0,p=0;
while(n!=0)
{
decimal+=((n%10)*Math.pow(2,p));
n=n/10;
p++;
}
System.out.println(decimal);
}
}
// 1. In the first method we will use parseInt() function to convert binary to decimal..
let binaryDigit = 11011; let getDecimal = parseInt(binaryDigit, 2);
console.log(getDecimal);
// 2. In the second method we going through step by step, using this trick we can improve our logic better..
let binaryDigit = 11011; let splitBinaryDigits =
String(binaryDigit).split(""); let revBinaryArr =
splitBinaryDigits.reverse(); let getDecimal = 0;
revBinaryArr.forEach((value, index) => {
let getPower = Math.pow(2, index);
getDecimal += value * getPower;
});
console.log(getDecimal);
我必须编写一个将二进制转换为十进制的方法。 编写一个方法,将提供的二进制数字(作为字符串)转换为十进制数。
- convertToDecimal("01101011") = 107
- convertToDecimal("00001011") = 11
我创建它是为了将十进制更改为二进制,但是我不确定如何将它创建为二进制到十进制。
public String convertToBinary(int decimal) {
int n = decimal;
int digit;
String out = "";
while (n > 0){
n = decimal/2;
digit = decimal % 2;
out = digit + out;
decimal = n;
}
out = addPadding(out);
return out;
}
private String addPadding(String s){
String out = s;
int len = s.length();
if (len == 8) return s;
else{
switch(len){
case 7:
out = "0"+s;
break;
case 6:
out = "00"+s;
break;
case 5:
out = "000"+s;
break;
}
}
return out;
}
}
看看怎么做,这里有详细的算法: https://javarevisited.blogspot.com/2015/01/how-to-convert-binary-number-to-decimal.html
建议的算法实现采用 int 作为输入。这是字符串版本:
public static int binaryToDecimal(String binary) {
int decimal = 0;
int power = 0;
int currentIndex = binary.length() - 1;
while (currentIndex>=0) {
int currentDigit = binary.charAt(currentIndex) - '0'; //char to number conversion
decimal += currentDigit * Math.pow(2, power);
power++;
currentIndex--;
}
return decimal;
}
需要将字符转换为数字,因为我们需要将字符“1”或“0”转换为数字 1 或 0。我们可以使用字符的 ascii 码(“0”=48和'1'=49)
1. We have to traverse from backwards as we do in the binary to decimal conversion.
2. If the character is '1', then we will add the value of power of 2.
public static int binaryToDecimal(String binary) {
int value = 0,power = 0;
for(int i = binary.length() - 1; i >= 0;i--) {
if(binary.charAt(i) == '1') {
value += Math.pow(2, power);
}
power++;
}
return value;
}
不确定您是否必须由您的老师手动完成,但如果不是,您可以使用一些可用的内置函数:
// Convert binary String to integer:
// TODO: Surround with try-catch
int outputInt = Integer.parseInt(inputBinaryString, 2);
// Convert integer to binary String:
String outputBinaryString = Integer.toBinaryString(inputInt);
// Pad leading zeros to make the length 8:
String paddedResult = String.format("%8s", outputBinaryString).replace(' ', '0');
试试这个代码
import java.util.Scanner;
class BinaryToDecimal
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a binary number:");
int n=s.nextInt();
int decimal=0,p=0;
while(n!=0)
{
decimal+=((n%10)*Math.pow(2,p));
n=n/10;
p++;
}
System.out.println(decimal);
}
}
// 1. In the first method we will use parseInt() function to convert binary to decimal..
let binaryDigit = 11011; let getDecimal = parseInt(binaryDigit, 2);
console.log(getDecimal);
// 2. In the second method we going through step by step, using this trick we can improve our logic better..
let binaryDigit = 11011; let splitBinaryDigits =
String(binaryDigit).split(""); let revBinaryArr =
splitBinaryDigits.reverse(); let getDecimal = 0;
revBinaryArr.forEach((value, index) => {
let getPower = Math.pow(2, index);
getDecimal += value * getPower;
});
console.log(getDecimal);