Convert byte[] to String

I

IveCal

Here is the code snippet. Can somebody help me convert byte arrays to
String? Here is the code snippet:

InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();

How will I convert byte[] ipAddr to String . . . ?
Please do reply . . .
Thanks . . .
 
J

JScoobyCed

IveCal said:
Here is the code snippet. Can somebody help me convert byte arrays to
String? Here is the code snippet:

InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();

How will I convert byte[] ipAddr to String . . . ?
Please do reply . . .
Thanks . . .

Errr...
String strIP = new String(ipAddr);

Is it what you mean?
 
D

Dale King

IveCal said:
Here is the code snippet. Can somebody help me convert byte arrays to
String? Here is the code snippet:

InetAddress addr = InetAddress.getLocalHost();
byte[] ipAddr = addr.getAddress();

How will I convert byte[] ipAddr to String . . . ?

You don't. InetAddress has a method (poorly named) to return the raw IP
address as a string:

String addressString = addr.getHostAddress();

You don't want to do the conversion yourself because some day you may be
dealing with IPV6 addresses which are 128-bit and do not use dots. If
you use the method above you will work with IPV4 and IPV6 and whatever
comes along after that.
 
A

Alan Krueger

Dale said:
You don't want to do the conversion yourself because some day you may be
dealing with IPV6 addresses which are 128-bit and do not use dots. If
you use the method above you will work with IPV4 and IPV6 and whatever
comes along after that.

Good point about the IPv6 addresses. Lots of colons and not so many dots.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,816
Latest member
nipsseyhussle

Latest Threads

Top