San*_*dey 1 java string integer type-conversion
我有4个字符串:
str1 = 10110011;(length of all string is:32)
str2 = 00110000;
str3 = 01011000;
str4 = 11110000;
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我必须添加这些字符串,结果应该是:
result[1] = str1[1]+str2[1]+str3[1]+str4[1];
Run Code Online (Sandbox Code Playgroud)
result
应该作为整数的加法获得.
对于上面的例子, result = 22341011
我知道Java中的整数到字符串转换非常容易,但我发现字符串到整数转换更难一点.
解析整数-2^31 < n < 2^31-1
使用:
Integer value = Integer.valueOf("10110011");
Run Code Online (Sandbox Code Playgroud)
对于较大的数字,请使用BigInteger类:
BigInteger value1 = new BigInteger("101100111011001110110011101100111011001110110011");
BigInteger value2 = // etc
BigInteger result = value1.add(value2).add(value3); //etc.
Run Code Online (Sandbox Code Playgroud)