Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strings transcoding in Java

I've found a piece of code recently, which does the following:

String s = ... // whatever
...
s = new String(s.getBytes(myEncoding), myEncoding);

For me it appears to be absolutely non-sense.

Is it possible that under certain circumstances (some specific combination of locale settings, used technologies, etc.), this code will do something useful?

Thanks in advance

like image 363
edio Avatar asked Apr 09 '26 09:04

edio


1 Answers

yes, that code is generally nonsense. yes, it's possible that that code could be doing "something" to the string (probably not anything good). generally speaking, if you have already incorrectly converted bytes to chars, trying to re-convert is rarely going to give you legitimate results. (there may be isolated instances where the right combination of character encodings may work).

like image 182
jtahlborn Avatar answered Apr 10 '26 21:04

jtahlborn