简体   繁体   English

如何在Java中将二进制字符串转换为十进制字符串

[英]How to convert a binary String to a decimal string in Java

I was working on a homework and thought I had finished but the teacher told me that it wasn't what he was looking for so I need to know how I can convert a binary number that is stored as a String to a decimal string without using any built-in function outside of length(), charAt(), power function, and floor/ceiling in Java. 我当时正在做作业,以为我完成了,但是老师告诉我这不是他要找的东西,所以我需要知道如何在不使用字符串的情况下将以字符串形式存储的二进制数转换为十进制字符串Java中length(),charAt(),power函数和floor / ceiling之外的任何内置函数。

This is what I had on the beginning. 这就是我刚开始的时候。

import java.util.Scanner;

public class inclass2Fall15Second {
    public static void convertBinaryToDecimalString() {
        Scanner myscnr = new Scanner(System.in);

        int decimal = 0;

        String binary;
        System.out.println("Please enter a binary number: ");
        binary = myscnr.nextLine();
        decimal = Integer.parseInt(binary, 2);
        System.out.println("The decimal number that corresponds to " + binary + " is " + decimal);
    }

    public static void main (String[] args) {
        convertBinaryToDecimalString();
    }
}

To convert a base 2 (binary) representation to base 10 (decimal), multiply the value of each bit with 2^(bit position) and sum the values. 要将基数2(二进制)表示转换为基数10(十进制),请将每个位的值与2 ^(位位置)相乘,然后求和。

eg 1011 -> (1 * 2^0) + (1 * 2^1) + (0 * 2^2) + (1 * 2^3) = 1 + 2 + 0 + 8 = 11 例如1011->(1 * 2 ^ 0)+(1 * 2 ^ 1)+(0 * 2 ^ 2)+(1 * 2 ^ 3)= 1 + 2 + 0 + 8 = 11

Since binary is read from right-to-left (ie LSB (least significant bit) is on the rightmost bit and MSB (most-significant-bit) is the leftmost bit), we traverse the string in reverse order. 由于二进制是从右向左读取的(即LSB(最低有效位)在最右边,MSB(最高有效位)在最左边),因此我们以相反的顺序遍历字符串。

To get the bit value, subtract '0' from the char. 要获取位值,请从字符中减去“ 0”。 This will subtract the ascii value of the character with the ascii value of '0', giving you the integer value of the bit. 这将用ASCII值“ 0”减去字符的ASCII值,从而获得该位的整数值。

To calculate 2^(bit position), we can keep a count of the bit position, and increment the count on each iteration. 要计算2 ^(位位置),我们可以保留位位置的计数,并在每次迭代时增加计数。 Then we can just do 1 << count to obtain the value for 2 ^ (bit position). 然后我们可以做1 <<计数以获得2 ^(位位置)的值。 Alternatively, you could do Math.pow(2, count) as well, but the former is more efficient, since its just a shift-left instruction. 另外,您也可以执行Math.pow(2,count),但是前者效率更高,因为它只是一个左移指令。

Here's the code that implements the above: 这是实现以上内容的代码:

public static int convertBinStrToInt(String binStr) {
    int dec = 0, count = 0;
    for (int i = binStr.length()-1; i >=0; i--) {
        dec += (binStr.charAt(i) - '0') * (1 << count++);
    }

    return dec;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中将由二进制数组成的字符串转换为十六进制十进制数? - How to convert a String consisting of Binary Number to Hexa Decimal no in Java? 在Java中将大二进制字符串转换为十进制 - Convert Large Binary String to Decimal in Java 如何在Java中将字符串转换为十进制 - How to convert string to decimal in Java 如何将二进制字符串值转换为十进制 - How to convert binary string value to decimal 如何将数字的二进制表示形式的Java链接列表转换为数字的十进制表示形式的字符串? - How to convert a java linked list of a binary representation of a number to a String of the decimal representation of a number? Java,如何将十进制数字作为字符串转换为整数 - Java, How to convert decimal number as string into an integer 如何在 java 中将十进制转换为二进制 - How to convert decimal to binary in java 如何在Java中将字符串转换为二进制utf-16和将二进制转换为字符串? - How to convert a string to binary utf-16 and Binary to String in java? 如何将二进制补码二进制字符串转换为负十进制数字? - how to convert twos complement binary string to negative decimal number? 如何在Java中将字符串的二进制表示转换为字节? - How to convert a binary representation of a string into byte in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM