-
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
-
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.
-
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