Converting byte array to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hari Prasad

    #1

    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);
    ByteArrayInputS tream byteIn = new ByteArrayInputS tream (dgram.getData
    (), 0, dgram.getLength ());

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

    String recvString = new String(dgram.ge tData(),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.
  • Somaiah Kumbera

    #2
    Re: Converting byte array to string

    Did I understand this completely wrong, or can't you just use the
    String(byte [] bytes) constructor?






    harispra@yahoo. com (Hari Prasad) wrote in message news:<257781cf. 0412010325.7447 f750@posting.go ogle.com>...[color=blue]
    > 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);
    > ByteArrayInputS tream byteIn = new ByteArrayInputS tream (dgram.getData
    > (), 0, dgram.getLength ());
    >
    > ObjectInputStre am oin = new ObjectInputStre am(byteIn);
    > Object obj = oin.readObject( );
    > String recvString = obj.toString();
    > DataInputStream dataIn = new DataInputStream (byteIn);
    > String recvString = dataIn.readLine ();
    >
    > String recvString = new String(dgram.ge tData(),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.[/color]

    Comment

    • Frank Gerlach

      #3
      Re: Converting byte array to string

      somaiah@gmail.c om (Somaiah Kumbera) wrote in message news:<d6303151. 0412021414.78f4 a102@posting.go ogle.com>...[color=blue]
      > Did I understand this completely wrong, or can't you just use the
      > String(byte [] bytes) constructor?[/color]
      "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.[color=blue]
      >
      >
      >
      >
      >
      >
      > harispra@yahoo. com (Hari Prasad) wrote in message news:<257781cf. 0412010325.7447 f750@posting.go ogle.com>...[color=green]
      > > 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);
      > > ByteArrayInputS tream byteIn = new ByteArrayInputS tream (dgram.getData
      > > (), 0, dgram.getLength ());
      > >
      > > ObjectInputStre am oin = new ObjectInputStre am(byteIn);
      > > Object obj = oin.readObject( );
      > > String recvString = obj.toString();
      > > DataInputStream dataIn = new DataInputStream (byteIn);
      > > String recvString = dataIn.readLine ();
      > >
      > > String recvString = new String(dgram.ge tData(),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.[/color][/color]

      Comment

      Working...