简体   繁体   English

使用CharAt()和length()将二进制转换为十进制

[英]binary to decimal using CharAt( ) and length( )

Ive written a program for converting binary to decimal, however I was wondering how can I do it without using Math.pow ( ) and by only using methods charAt ( ) and length() 我已经编写了将二进制转换为十进制的程序,但是我想知道如何在不使用Math.pow()且仅使用charAt()和length()方法的情况下做到这一点

thank you,, any help would be appreciated 谢谢,任何帮助将不胜感激

public class BinaryToDecimal{

    public static void main(String[] args){
        String binaryString = args[0];
        int decimalValue;

        int length = binaryString.length();
        double j = 0;

        for (int i=0; i< binaryString.length(); i++){
            if (binaryString.charAt(i)== '1'){
                j = j+  Math.pow(2, binaryString.length() - 1 - i);
            }
        }

        decimalValue = (int) j;   

        System.out.println(decimalValue);
    }
}

You can try this: 您可以尝试以下方法:

int j = 0;
for (int i=0; i< binaryString.length(); i++)
{
  j <<= 1;
  if (binaryString.charAt(i)== '1')
  {
    ++j;
  }
}

Yes, you can (and should) do the conversions without using Math.pow . 是的,您可以(并且应该)不使用Math.pow进行转换。

The following code adds the nth power of 2 to j : 以下代码将2的n次幂加到j

j += 1 << n;

But I recommend the answer by Jim Rhodes, because it is a simpler solution. 但我建议吉姆·罗德斯(Jim Rhodes)给出答案,因为这是一个更简单的解决方案。 You do not need to raise 2 to various powers; 您无需将2提升至各种异能。 all you really need to know is that every time you write an extra digit on the right-hand end of a binary number, the value of the previous digits is doubled. 您真正需要知道的是,每当您在二进制数的右端写一个额外的数字时,前一个数字的值就会加倍。 For example, 111 in binary equals the decimal number 7, but if I write one more digit, like so, 1110 , then the value becomes equal to 2*7, that is, 14 in decimal notation. 例如,二进制的111等于十进制数7,但是如果我再写一个数字,如1110 ,则该值等于2 * 7,即十进制表示法14。

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

相关问题 Java:将.split用于重载分隔符(仅使用charAt()和length()) - Java: Using .split for Delimiters with Overloading (using charAt() and length() only) 如何使用 charAt 和 string.length() 拆分字符串 - how to split a string by using charAt and string.length() Java搜索文本文件仅使用charAt()和length()来查找字符串 - Java search text file for string only using charAt() and length() 我们如何在不使用任何内置函数的情况下找到Java中String的长度? (甚至不包括charAt()或toCharArray()或length()) - How can we find the length of a String in Java without using any inbuilt functions? (not even charAt() or toCharArray() or length()) 使用递归将十进制转换为二进制 - Convert decimal to binary using recursion 使用for循环将小数转换为二进制? - Coverting a decimal to binary using for loops? 使用递归将十进制转换为二进制 - Conversion of decimal into binary using recursion 仅使用 class 字符串的下一个方法:charAt(),substring(),length() - Finding a substring of a string with recursion using only the next Methods of class String : charAt(),substring(),length() 使用charAt代替子字符串 - Using charAt instead of substring 如何在Java中仅使用charAt(),length()和/或toCharArray()从字符串中提取数字 - How can I extract the numbers from a string only using charAt(), length() and/or toCharArray() in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM