Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Why does getBytes() truncate the string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does getBytes() truncate the string?" Watch "Why does getBytes() truncate the string?" New topic
Author

Why does getBytes() truncate the string?

Diego Klappenbach
Ranch Hand

Joined: Aug 29, 2003
Posts: 31
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Diego Klappenbach
Ranch Hand

Joined: Aug 29, 2003
Posts: 31
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Dec 10, 2001
Posts: 7023
Just in case ya'll are in the mood to talk more on this subject, I'm moving this to the Intermediate forum...
[ August 30, 2003: Message edited by: Dirk Schreckmann ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does getBytes() truncate the string?