5

I currently am trying to create a chat server as an assignment and want each message to contain a header. It will contain ipv4 address followed by a letter then a username

I can easily decode string letters from bytes but now I am struggling to decode an ipv4 address from bytes

the representation so far from the bytes is this

[-64, -88, 1, 5]

which in the ipv4 dotted quad format would be 192.168.1.5

I just need a way to try and decode the four bytes of integers to a string or something along those lines

THANKS :D

2 Answers 2

12

InetAddress.getByAddress(bytes).getHostAddress()?

Sign up to request clarification or add additional context in comments.

Comments

0

That is easily done like this:

byte[] address = ...;
String addressStr = "";
for (int i = 0; i < 4; ++i)
{
    int t = 0xFF & address[i];
    addressStr += "." + t;
}
addressStr = addressStr.substring(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.