First thing is that you need to make sure that your source file is actually stored as UTF-8
- see @Ankur's answer for a good explanation.
Then, you also need to provide an encoding when calling getBytes()
on String
to retrieve the byte array:
byte sByte[] = Cdata.getBytes("UTF-8");
If you call String.getBytes()
with no encoding, the platform`s default encoding is used, which can be (almost) anything. See also java.lang.String.getBytes():
Encodes this String into a sequence of bytes using the platform's default charset
With that, the following SSCCE properly prints the expected output for me (note: took identifiers from question, not adjusted to coding conventions):
import java.io.UnsupportedEncodingException;
public class Encoding {
public static void main(String[] args) throws UnsupportedEncodingException {
String Cdata = "MARIE-HÉLÈNE";
byte sByte[] = Cdata.getBytes("UTF-8");
Cdata = new String(sByte,"UTF-8");
System.out.println(Cdata);
}
}