简体   繁体   中英

How to convert chinese characters to hexadecimal in java

I want to convert unicode characters which have code values in 2 byte, 3 byte range to hexadecimal.

I know how to convert characters to hexadecimal. For which, I am first converting characters to ascii by casting using int. And from that i convert to hexadecimal using Integer.tohexString.

However, I am not sure how to deal with unicode characters like chinese, etc. Can someone help?

You should not handle the String as an array of bytes in Java... But if you want, you can use getBytes()

byte[] bs = "中文".getBytes("utf-8");
for(byte b : bs)
    System.out.print(Integer.toHexString(b) + " ");

See my comment .

Don't "convert to ASCII"; Java char is big enough to support the entire Basic Multilingual Plane , which should contain CJK Han script.

/* 七 -> 4e03 */
assert "4e03".equals(Integer.toHexString('七'));

I think you might want to rethink your approach.

In general, it sounds like you want to do some sort of I/O of this data. In general, you do that with character encodings and the java.io Reader/Writer API.

See InputStreamReader and OutputStreamWriter.

You can always send the output of a writer to a FileOutputStream (or ByteArrayOutputStream for that matter) to get the characters out as binary data.

out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("file"), "utf-8"));
out.println("...");

If you want to then base64 encode that data or just represent it as ascii hex, that is a pretty simple thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM