so have binary string (32 chars corresponding 32 bits):
string s = "11111111110000011101110111011000";
when try convert number, throws numberformatexception:
integer.parseint(s,2);
could not figure out why. idea? strange thing if replace first bit in string left (1) 0 bit (0), works. first bit indicating sign of number, should not make overflow of 4 bytes in here, right?
=================
updated question: answers. got now; parseint() method cannot handle sign bit , assumes sign bit represents value other bit. question how convert such binary string signed integer number? know of suggested use long, in situation need int (long explain, can assume that).
the number you're trying parse bigger 2^31-1
(max integer), use long instead:
string s = "11111111110000011101110111011000"; system.out.println(long.parselong(s, 2)); // 4,290,895,320
if want see what's maximum value of integer can do:
system.out.println(integer.max_value); // 2,147,483,647
responding update section, if don't mind loosing information (which happens when convert long int) can do:
string s = "11111111110000011101110111011000"; long l = long.parselong(s,2); int = (int)l; system.out.println(i); // -4071976
Comments
Post a Comment