Results 1 to 3 of 3
Thread: String from byte array
- 01-22-2010, 04:38 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
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
- 01-22-2010, 05:24 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 01-22-2010, 06:58 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 2
- Rep Power
- 0
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
-
Search a string in a byte array
By 2BOrNot2B in forum New To JavaReplies: 0Last Post: 03-12-2009, 05:52 PM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
Printing Byte Array
By suchismitasuchi in forum New To JavaReplies: 3Last Post: 01-19-2009, 10:58 AM -
Byte Array
By sandor in forum New To JavaReplies: 12Last Post: 01-15-2009, 03:31 AM -
String byte storage
By bozovilla in forum New To JavaReplies: 1Last Post: 11-24-2007, 06:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks