Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String method

 
Qusay Jaafar
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1- How can I convert a deprecated String constructor which is:
String(byte[] ascii, int hibyte, int offset, int count)
to another non-deprecated String constructor?
2- How can I convert a deprecated String method which is:
getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)
to another non-deprecated String method?
I appreciate if your explination with a code
Regards
Q. Jaafar
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaffer,
1. If this is for the developer certification, then this question should probably be asked in the developer's forum.
2. IMO, this is the sort of thing you should look up. It will only take about five minutes to figure out.
M
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. (To Max) Well, maybe. But at this point, it's been asked in a bunch of other forums as well, and others have already chosen to redirect some of the other forums here, so I'd choosing to answer it here for consistency.
(To Quasi) If the value of hibyte used was 0, then the simplest solution is to use the String(byte[], int, int, int) constructor instead:
new String(ascii, offset, count, "ISO-8859-1")
since ISO-8859-1 directly Unicode encoding for values 0-0xFF. Also worth trying is
new String(ascii, offset, count)
since it's reasonably possible the original code simply meant to use the platform default encoding, but didn't get it quite right. This typically makes little difference for "normal" ascii values - but look for funny chars like ™ and ‰ (if they are used anywhere) to see if they're coming out OK or not.
If the value of hibyte is anything other than 0, read the code very carefully and try to determine the author's actual intent. Consider the possibility that they were on crack at the time, and their decoding method may simply be fundamentally wrong. Was there some standard named encoding scheme they were trying to replicate? If so, it might be better to learn the name of the encoding, and use one of the String constructors that accepts a named encoding. If it's really necessary to replicate the original functionality exactly, you can read the description for the deprecated method carefully, and write your own code which creates a char[] array and populates it with chars created from the byte[] array as described (taking hibyte into account). Then use one of the non-deprecated constructors to make a String from the char[].
2. Generally, the answer to this question is probably based on the answer to 1. Whichever constructor/encoding combination turns out to work best above, there's a corresponging getBytes() method which can reverse the process.
[ July 17, 2002: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic