Interesting Integer.parseInt() problem



I'm working on a project where I'm being given a string of 0s and 1s and need to mask them against another string of the same. I thought I would convert my string with Integer.parseInt("111",2) but I ran into a problem. You can't parse out 32 bits to an int if the MSB is 1. It throws a NumberFormatException. You can't parse -2147483648 in either radix 2, 8 or 16. My data is of course LSB first so 1 would be 100000000000000000000000000000000 and I can't parse it. Anyway, I thought it was interesting and wondered if anybody knows why this is so?

Thanks,

knute...


public class test { public static void main(String[] args) { // String str = "1111111111111111111111111111111"; // String str = "7fffffff"; // String str = "17777777777"; int n = Integer.parseInt(str,2); System.out.println(n); } }

--

Knute Johnson
email s/nospam/knute/
.



Relevant Pages

  • [PATCH] bitmap parsing routines, version 3
    ... int bitmap_weight(const unsigned long *bitmap, ... * bitmap_snprintf - convert bitmap to a hex string. ... * and a length, describing the mask contents. ...
    (Linux-Kernel)
  • [PATCH] bitmap parsing/printing routines, version 4
    ... This release of the bitmap parser/printer continues the cycle of making ... I gradually came to feel that examining the whole string was important, ... int bitmap_weight ... * and a length, describing the mask contents. ...
    (Linux-Kernel)
  • Re: Help With PyParsing of output from win32pdhutil.ShowAllProcesses()
    ... None to a string). ... Still have an urge to parse with pyparsing? ... pyparsing Dict class. ... keys from the parsed data, so it looks like you are already thinking ...
    (comp.lang.python)
  • Re: [C] please critique my code
    ... Why have both a mask and an index? ... Another thing worth noting is that the string length is known in ... If you want to pad on the ... std::string uint2bin(unsigned int value, char pad) ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Pyparsing help
    ... our grammar isn't parsing the METAL2 entry at all. ... input string". ... successful parse, then restored half of the entries I removed, until I ... Layer PRBOUNDARY ...
    (comp.lang.python)

Loading