Error in binary String to Integer converter

Croatia Boy

I have binary String To Integer method implemented in my code. Problem is that I'm getting wrong last two digits of method output results, e.g.:

SHOULD BE RESULT : 11111111100001001000101110000100

RESULT : 11111111100001001000101110000011

There is always problem with last two digits of result.

Any help would be greatly appreciated.

code:

public static int binaryStringToInteger (String binaryString){
char[] digits = binaryString.toCharArray();
int binaryInteger = 0;
int count = 0;
for(int i=digits.length-1;i>=0;i--)
  {
    if(digits[i]=='1') 
         {
          binaryInteger +=(int)Math.pow(2, count);
         }
    count++;
  }
 return binaryInteger;
}
ppDemo

There is nothing wrong with your code except int type can only hold so much. Int holds 4 bytes that is 8bitsx4 = 32 bits with the first one reserved for sign, negative if 1 and positive if 0. Try using long int which holds 64 bits:

public static long binaryStringToInteger (String binaryString){
    char[] digits = binaryString.toCharArray();
    long binaryInteger = 0;
    int count = 0;
    for(int i=digits.length-1;i>=0;i--)
      {
        if(digits[i]=='1') 
             {
              binaryInteger +=(long)Math.pow(2, count);
             }
        count++;
      }
     return binaryInteger;
    }

Also check this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting a binary string to integer

Binary String to Integer Stack

string in binary format to integer

integer to string converter(using macros)

Turning a binary in string form to an integer

How to Converter String to Integer having dots in java

Broken binary converter. Index error

Converting String binary literal into Integer [Java]

Browser read integer from binary string

Convert binary/hex encoded string to an integer

How to convert a binary integer number to a hex string?

Swift. Get binary string from an integer

Is there anything in Rust to convert a binary string to an integer?

Get signed integer from swift string of binary

hex to binary string and integer harmony needed

converting a string read from binary file to integer

Convert an integer to a binary string with leading zeros

How to convert string and integer to binary in nodejs?

binascii.hexlify() returns binary as String and not Integer

Fetch error list with integer to binary conversion

Convert binary (0|1) numpy to integer or binary-string?

Integer to String error in my program

When I put a number larger than 1 in my binary-to-decimal converter, I get the error startIndex cannot be larger than length of string

Regex: Binary string contains at least 3 of a certain integer

Converting 32-bit binary string with Integer.parseInt fails

How to convert a Binary String to a base 10 integer in Java

Change Binary String to Integer Through Every two character separation

Converting an integer to signed 2's complement binary string

TC:L: Convert an integer into binary string of arbitrary length