Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Byte returning negative value??

Status
Not open for further replies.

DanglingPointer

Programmer
Apr 5, 2003
22
GB
Whats up with this?

I want to use a plain old unsigned byte 0-255, but the default Java byte type and Byte object are both signed, meaning when I use values > 200 I get value - 256.

I suppose this is something to do with how the byte represents a negative (2s complement?) but i'm not sure of the details.

the code fragment i'm using:

InetAddress ia = null;
byte[] bip = new byte[4];
Byte btmp = null;
String ip = new String();

ia = InetAddress.getByName(hostName);
bip = ia.getAddress();
for ( int i = 0; i < 4; i++ ) {
btmp = new Byte(bip);
if ( i != 3 )
ip += btmp.toString() + &quot;.&quot;;
else
ip += btmp.toString();
}

CHeers.
 
Please, put your code between [ code ] and [ /code ], otherwhise
Code:
 [i]
gets interpreted as &quot;show in italics&quot; by the forum.
What is the problem? What do you want to do with those bytes ?
 
i answered this question a while ago.

thread269-580099

~za~
You can't bring back a dead thread!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top