• Post Reply Bookmark Topic Watch Topic
  • New Topic

InetAddress.getByName returns negative IP value???  RSS feed

 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but I got negative IP values, why???
 
Alexandru Popescu
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls read attentively the API for getAddress() (raw IP address: highest order byte...). What you probably need is getHostAddress()

--
:alex |.::the_mindstorm::.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HaoZhe Zhu --

The range for a Java byte is from -128 to 127; they're signed. But if you use a mask to transfer a byte into an int, you can get the unsigned value -- i.e.,

byte b = -1;
int i = b & 0xFF;

The variable "i" will be 255, the unsigned equivalent of the value -1 that was stored in "b".
 
HaoZhe Xu
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, still a question: InetAddress.getByAddress(byte [] address) can only accept byte array, so after using b & 0xff, how to make it to byte?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!