This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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'.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why the ip address value is and with int 255
 
Similar Threads
I made an ARP with java but it do not work properly
Finding Nth Largest element of an array without sorting
Why Won't a Simple Socket Program Work when a Windows 7 Machine Acts as Server?
Finding All the IP Addresses to a LAN
InetAddress class