1

I have 4 strings:

 str1 = 10110011;(length of all string is:32)
 str2 = 00110000;
 str3 = 01011000;
 str4 = 11110000;

In my project I have to add these string and the result should be:

result[1] = str1[1]+str2[1]+str3[1]+str4[1];

result should be obtained as addition of integer numbers.

For the example above, result = 22341011

I know integer to string conversion in Java is very easy but I found string to integer conversion a little harder.

3
  • possible duplicate of How to convert string to int in Java? Commented Aug 15, 2012 at 20:08
  • thanks for answer. I forget to say that my length of string is 32. then integer.valueOf() doesn't works. :( Commented Aug 15, 2012 at 20:10
  • Are you actually trying to convert binary to integers? Do the strings only contain 0 and 1? Do they represent signed or unsigned numbers? Do the numbers fit in an int? The maximum number of an int is 2 billion and some, so a number of 32 characters does not fit in an int. Commented Aug 15, 2012 at 20:12

4 Answers 4

7

To parse Integers -2^31 < n < 2^31-1 use:

Integer value = Integer.valueOf("10110011");

For numbers that are larger, use the BigInteger class:

BigInteger value1 = new BigInteger("101100111011001110110011101100111011001110110011");
BigInteger value2 = // etc
BigInteger result = value1.add(value2).add(value3); //etc.
Sign up to request clarification or add additional context in comments.

3 Comments

yes this is good answer but actually when string length is 32 then this doesn't works. @ggreiner
BigInteger is object? That means, i should define myself or it is predefined object in java?
BigInteger is a class, take a look at the documentation.
2

The simplest way to do this is with Integer.parseInt(str1). Returns an int containing the value represented by the string.

valueOf() returns an Integer object, rather than an int primitive.

1 Comment

but autoboxing will take care of that anyway so it's not a problem. int x = Integer.valueOf("123"); will work just fine thanks to autoboxing.
1

Because your numbers are so big they will not fit in an int. Use the BigInteger class.

Comments

0

I am not known about your project and what actually your problem is. But I came to guess from your partial information that, you have multiple set of strings in bit representation as you explained.

str1 = "1000110.....11";
str1 = "1110110.....01"; etc

adding those decimal values,gives an ambiguous result as an integer can be the sum of multiple integer values. Just see an example below where there are total 5 possibilities[with positive decimal values] to yield 6.

1+5 = 6;
2+4 = 6;
3+3 = 6;
4+2 = 6;
5+1 = 6;

If you proceed in that way you just do an error,nothing else in your case. One better solution can be, compute the decimal values of individual strings. Instead of adding(+) them, just concat(join) them to form a single string. I am suggesting this approach because, This gives always a unique value and later you may need to know individual strings decimal values.

String strVal1 = String.format(computeDecimal(str1));
String strVal2 = String.format(computeDecimal(str2));
String strVal3 = String.format(computeDecimal(str3));
.
.
.
String strValn = String.format(computeDecimal(strn));

String myVal = String.concate(strVal1,strVal1,strVal1,....strValn);

Now you can treat your string as your wish.

//This will give you a non conflicting result.

Better to implement above approach than BigIntegers.

Hope this helps you greatly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.