Results 1 to 2 of 2
Thread: Byte value comes out negative
- 07-03-2010, 01:32 AM #1Member
- Join Date
- Feb 2008
- Oregon, USA
- Rep Power
[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 Byte.valueOf(ipAddr).intValue(); Integer.valueOf(ipAddr);
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
Last edited by Bluefox815; 07-03-2010 at 01:59 AM.
- 07-03-2010, 03:52 AM #2
- By ea25 in forum New To JavaReplies: 1Last Post: 04-21-2010, 02:28 AM
- By pendulum101 in forum New To JavaReplies: 5Last Post: 11-01-2009, 05:47 PM
- By anthonym2121 in forum New To JavaReplies: 1Last Post: 04-07-2009, 08:06 AM
- By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM
- By random0munky in forum New To JavaReplies: 9Last Post: 12-08-2008, 11:17 PM