Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does getBytes() truncate the string?

 
Diego Klappenbach
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to convert a string into bytes. I've read there are two alternatives:
a)
String s;
byte[] buf = new byte[ s.length() + 1 ];
s.getBytes( 0, s.length(), buf, 0 );
or b)
String s;
byte[] buf = s.getBytes();
whereby (a) is deprecated, but the preferred way to do it, namely (b), truncates the string.
Why does the string get truncated in (b)?
And is there some way of doing this (without using deprecated methods)?
Regards,
Diego
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Diego,
Welcome to JavaRanch!
Can you show us an example where getBytes() truncates the String -- normally, of course, it doesn't, so I'd like to see an example of what you're talking about.
I suspect, though, that maybe you're expecting a 0 byte at the end, like a C string would have, and since there isn't one, that's what you mean by truncated. There won't be one in Java; there's no need, since you can ask any array for its length using the "length" member variable.
 
Diego Klappenbach
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ernest, thanks for the swift reply,
I got the example from Bruce Eckels book "Thinking in Java" (first edition, chapter 15: Network Programming, section "Datagrams").
See
http://www.codeguru.com/java/tij/tij0166.shtml
in the class "Dgram".
Actually, I wasn't expecting a 0 byte at the end - to be frank, I wasn't expecting anyting!
I'm still pretty new at this...to me, "truncated" just means that the string will be chopped off and you won't get the last character or something like that.
Thanks,
Diego
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, OK. Here Bruce is apparently talking about a bug in some early version of JDK 1.1, as if that bug were relevant to the reader. Considering that 1.1 ran to 8 minor versions, followed by many versions of JDK 1.2, then 1.3, then 1.4, and 1.5 (which will come out very soon), you'll perhaps appreciate that this discussion is somewhat dated! JDK 1.1 came out in 1997 -- six years ago. As you can imagine, a lot has changed since then.
The URL you give points, apparently, to a very old edition of Eckel's book. Here's a reference to the third edition free download page; I hope this (apprently sadly outdated) section has been fixed!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case ya'll are in the mood to talk more on this subject, I'm moving this to the Intermediate forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic