469,270 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

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.
Jul 17 '05 #1
2 119965
Did I understand this completely wrong, or can't you just use the
String(byte [] bytes) constructor?


ha******@yahoo.com (Hari Prasad) wrote in message news:<25**************************@posting.google. 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.

Jul 17 '05 #2
so*****@gmail.com (Somaiah Kumbera) wrote in message news:<d6**************************@posting.google. 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.

ha******@yahoo.com (Hari Prasad) wrote in message news:<25**************************@posting.google. 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.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt melton | last post: by
4 posts views Thread by Hal Vaughan | last post: by
3 posts views Thread by Pete Davis | last post: by
4 posts views Thread by Prabhu | last post: by
1 post views Thread by Benoit | last post: by
16 posts views Thread by manmit.walia | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.