Can I somehow use 0x notation with long

3

Working with SMPP protocol I used long to represent values of system_id field of type unsigned int (4 bytes). In protocol itself field values are specified in hexademical format. So I wanted to preserve it in my code. While trying to compare the variable value i wrote:

long id = ...;
//...
if(id == 0x80000009){...}

And found out that java interprets 0x80000009 as int, which is signed 4 byte int, so an overflow takes place and the comparison fails:

id         == 2147483657
0x80000009 ==-2147483639

Then I tried explicit type casting, i.e. id == (long)0x80000009 - same result. Then I declared variable like this:

static final long bind_transceiver_resp = 0x80000009;

But it's still an overflowed int value.

So is there a way to use 0x notation with values greater than Integer.MAX_VALUE?

java
asked on Stack Overflow Jan 22, 2014 by horgh

1 Answer

6

Put an L on the end of the literal. 0x80000009L is 2147483657. Note that a lower case l works too, but I don't recommend it because in many fonts, it looks like the number one.

answered on Stack Overflow Jan 22, 2014 by Dawood ibn Kareem

User contributions licensed under CC BY-SA 3.0