On Thu, 27 May 2004 16:16:07 +0800, jackie wrote:
public static String toGBString( String asciiStr )
{
String s = "";
try {
rtnStr = new String( ascii.getBytes(), "gb2312" );
} catch( Exception ex ) {}
return s;
}
I am not sure if this kind of routine require to be synchronized.
Your method is nonsensical for several reasons:
- there does not appear to be any variable called "ascii" in scope,
and you have ignored the "asciiStr" argument passed to the method.
- you throw away rtnStr and always return "".
- Strings in Java use unicode. There is no "character encoding"
involved, so the conversion you are attempting is not meaningful.
Character encoding is part of the conversion to and from byte
arrays. It says how the byte array should be interpreted, and has
nothing to do with the Strings themselves.
- ascii.getBytes() does not specify what character encoding should be
used for the conversion to byte[], so the resulting bytes you get
might not even be ASCII, depending on the default character encoding
on your platform. Always specify a character encoding when
converting from String to byte[], or from byte[] to String.
- if you have a byte[] containing the ASCII representation of some
characters, then you should specify ASCII when converting it to a
String. If you take a byte[] containing some given representation of
a String and convert it to a String using a different character
encoding, the results will not be meaningful.
Finally, since your method does not modify any variables external to
the method itself, there is no synchronization necessary. However this
is hardly your first concern here.
/gordon
--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e