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.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
MrYo
  • 1,797
  • 3
  • 19
  • 33
  • possible duplicate of [How to convert string to int in Java?](http://stackoverflow.com/questions/5585779/how-to-convert-string-to-int-in-java) – Sjoerd Aug 15 '12 at 20:08
  • thanks for answer. I forget to say that my length of string is 32. then integer.valueOf() doesn't works. :( – MrYo Aug 15 '12 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. – Sjoerd Aug 15 '12 at 20:12

4 Answers4

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.
driangle
  • 11,601
  • 5
  • 47
  • 54
  • yes this is good answer but actually when string length is 32 then this doesn't works. @ggreiner – MrYo Aug 15 '12 at 20:13
  • BigInteger is object? That means, i should define myself or it is predefined object in java? – MrYo Aug 15 '12 at 20:18
  • [BigInteger](http://docs.oracle.com/javase/1.5.0/docs/api/java/math/BigInteger.html) is a class, take a look at the documentation. – driangle Aug 15 '12 at 20:19
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.

ewok
  • 20,148
  • 51
  • 149
  • 254
  • 1
    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. – Steve Atkinson Aug 15 '12 at 20:38
1

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

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
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.

ln2khanal
  • 949
  • 1
  • 8
  • 16