Converting byte array to string  
Author Message
harispra





PostPosted: 2004-12-1 19:26:00 Top

java-programmer, Converting byte array to string Hi,
I am trying to convert a byte array to a string. I am using the
possible ways
but not getting the result.

DatagramPacket dgram = new DatagramPacket(buf, buf.length);
ByteArrayInputStream byteIn = new ByteArrayInputStream (dgram.getData
(), 0, dgram.getLength ());

ObjectInputStream oin = new ObjectInputStream(byteIn);
Object obj = oin.readObject();
String recvString = obj.toString();
DataInputStream dataIn = new DataInputStream (byteIn);
String recvString = dataIn.readLine();

String recvString = new String(dgram.getData(),0,dgram.getLength());
String recvString = dataIn.readLine();

I am trying all these to convert my byte array to a string
But I am getting exception as invalid header.

Can anybody please suggest me.

Regards,
Hari.
 
Lothar Kimmeringer





PostPosted: 2004-12-1 20:17:00 Top

java-programmer >> Converting byte array to string On 1 Dec 2004 03:25:52 -0800, Hari Prasad wrote:

> DatagramPacket dgram = new DatagramPacket(buf, buf.length);
> ByteArrayInputStream byteIn = new ByteArrayInputStream (dgram.getData
> (), 0, dgram.getLength ());
>
> ObjectInputStream oin = new ObjectInputStream(byteIn);
> Object obj = oin.readObject();
> String recvString = obj.toString();
> DataInputStream dataIn = new DataInputStream (byteIn);
> String recvString = dataIn.readLine();
>
> String recvString = new String(dgram.getData(),0,dgram.getLength());
> String recvString = dataIn.readLine();
>
> I am trying all these to convert my byte array to a string
> But I am getting exception as invalid header.

How is the String put into the UDP-packet? Is it really
the String-Object serialized or is it just the text?

If the latter, then a simple

String recvString = new String(dgram.getData());
Maybe the explicit specification of the encoding to be used
might be considered in addition to that.

should be enough.

BTW: comp.lang.java doesn't exist anymore.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: email***@***.com
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
somaiah





PostPosted: 2004-12-3 6:14:00 Top

java-programmer >> Converting byte array to string Did I understand this completely wrong, or can't you just use the
String(byte [] bytes) constructor?






email***@***.com (Hari Prasad) wrote in message news:<email***@***.com>...
> Hi,
> I am trying to convert a byte array to a string. I am using the
> possible ways
> but not getting the result.
>
> DatagramPacket dgram = new DatagramPacket(buf, buf.length);
> ByteArrayInputStream byteIn = new ByteArrayInputStream (dgram.getData
> (), 0, dgram.getLength ());
>
> ObjectInputStream oin = new ObjectInputStream(byteIn);
> Object obj = oin.readObject();
> String recvString = obj.toString();
> DataInputStream dataIn = new DataInputStream (byteIn);
> String recvString = dataIn.readLine();
>
> String recvString = new String(dgram.getData(),0,dgram.getLength());
> String recvString = dataIn.readLine();
>
> I am trying all these to convert my byte array to a string
> But I am getting exception as invalid header.
>
> Can anybody please suggest me.
>
> Regards,
> Hari.
 
 
frankgerlach22





PostPosted: 2004-12-4 9:27:00 Top

java-programmer >> Converting byte array to string email***@***.com (Somaiah Kumbera) wrote in message news:<email***@***.com>...
> Did I understand this completely wrong, or can't you just use the
> String(byte [] bytes) constructor?
"just" is the critical word. If you use String(byte [] bytes), the default
encoding will be chosen, which is dependant on you locale.
You should use String(byte [] bytes,String encoding) instead; this makes
the used encoding explicit and reproducable on different machines.
example: new String(ba,"utf-8")
This assumes your data is utf-8 encoded.
google the web for "java encoding" encoding to see which encodings are
avaible.
>
>
>
>
>
>
> email***@***.com (Hari Prasad) wrote in message news:<email***@***.com>...
> > Hi,
> > I am trying to convert a byte array to a string. I am using the
> > possible ways
> > but not getting the result.
> >
> > DatagramPacket dgram = new DatagramPacket(buf, buf.length);
> > ByteArrayInputStream byteIn = new ByteArrayInputStream (dgram.getData
> > (), 0, dgram.getLength ());
> >
> > ObjectInputStream oin = new ObjectInputStream(byteIn);
> > Object obj = oin.readObject();
> > String recvString = obj.toString();
> > DataInputStream dataIn = new DataInputStream (byteIn);
> > String recvString = dataIn.readLine();
> >
> > String recvString = new String(dgram.getData(),0,dgram.getLength());
> > String recvString = dataIn.readLine();
> >
> > I am trying all these to convert my byte array to a string
> > But I am getting exception as invalid header.
> >
> > Can anybody please suggest me.
> >
> > Regards,
> > Hari.