File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes Why the ip address value is and with int 255 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Why the ip address value is and with int 255" Watch "Why the ip address value is and with int 255" New topic
Author

Why the ip address value is and with int 255

Apoorva Dubey
Greenhorn

Joined: Sep 25, 2013
Posts: 1
Hello I have a simple program to find my ip address and converting it to String.
I am unable to understand why the `b[i]` value is and with `(int)255` below.

Luke Leber
Ranch Hand

Joined: Sep 20, 2013
Posts: 41
    
    2

You're working with a byte array (whose integral elements range from -128 to 127) IP address octets are generally represented as unsigned values (no negatives).

In order to properly 'unsign' a byte, you can apply a bitwise operation to it that goes hand in hand with the implicit widening of the data-type to 'int'.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why the ip address value is and with int 255