Results 1 to 3 of 3
  1. #1
    justint is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default String from byte array

    Hi, I'm using

    DataInputStream dis = new DataInputStream(socket.getInputStream());

    data = new byte[2048];
    dis.read(data);

    String text = new String(data);

    System.out.println("Received: " + text.length());

    I type in "text"

    I get "2048" for the length, the size of the byte array. How can I have the length of the string be only the length of what i input, "text", which should give me 4?

    Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    I don't quite know what you mean by I type in "text" as you don't give code that relates to that. However you can see the string that corresponds to the bytes in the stream by printing it. Perhaps that will provide information.

    Sun's Tutorial has a discussion how different data types (including strings) are written to and read from such streams in the section Data Streams.

  3. #3
    justint is offline Member
    Join Date
    Jan 2010
    Posts
    2
    Rep Power
    0

    Default

    I'm trying to make a server, I telnet into that server via console and type "text", hit enter, and send it off to the server. The server: serverSocket.accept()...then I get the socket's inputStream.

    I'm not sure if this clarifies the problem, I will post code when i get home, if nobody can help me by then.

    Thanks

Similar Threads

  1. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 06:52 PM
  2. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  3. Printing Byte Array
    By suchismitasuchi in forum New To Java
    Replies: 3
    Last Post: 01-19-2009, 11:58 AM
  4. Byte Array
    By sandor in forum New To Java
    Replies: 12
    Last Post: 01-15-2009, 04:31 AM
  5. String byte storage
    By bozovilla in forum New To Java
    Replies: 1
    Last Post: 11-24-2007, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •