[SOLVED] Byte value comes out negative
I'm having some trouble with getting an IP address to come out in all positive numbers. Here is the code I'm using:
// host and port are given by the user
Socket socket = new Socket(host, port);
byte ipAddr = socket.getInetAddress().getAddress();
String hostAddr = ipAddr + "." + ipAddr + "." + ipAddr + "." + ipAddr;
String hostDir = "data/" + hostAddr;
The socket object made a connection without a problem. But other parts of the program are failing because if any of the 4 pieces of the IP addresses (also known as "octets") are over 127, then the number comes out negative because apparently a 'byte' in Java is signed.
I have tried converting the number to an 'int' value in an attempt to make it seem unsigned, but it still came out negative.
// these didn't work
Any help on converting a negative byte to the corresponding positive number is appreciated.
EDIT: Convert an UNSIGNED byte to a JAVA integer - Real's Java How-to