• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why does getBytes() truncate the string?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
[ August 30, 2003: Message edited by: Dirk Schreckmann ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic