How to convert special characters to hex? ( in Android )

血红的双手。 提交于 2019-12-12 05:03:13

问题


Convert some special characters into hex value.

For example :

Hex value of "ㅂ" is "e3 85 82"

Hex value of "ㅈ", "ㄷ", and "ㄱ" are "e3 85 88", "e3 84 b7", and "e3 84 b1" respectively.

I tried below method but it works only for ";", "#" etc

Integer.toHexString("ㅂ") , gives "3142" value. But correct hex value should be "e3 85 82".

String to Hex


回答1:


Your answer encoding is in UNICODE(hex) and you need to convert it into UTF8(hex)

Convert String to hex.

public static void main(String[] args) throws UnsupportedEncodingException {
    String chr = "ㅂ";
    System.out.print(toHex(chr));
}
//String to hex
public static String toHex(String arg) throws UnsupportedEncodingException {
    //Change encoding according to your need 
    return String.format("%04x", new BigInteger(1, arg.getBytes("UTF8")));
}

Output:- e38582



来源:https://stackoverflow.com/questions/25120784/how-to-convert-special-characters-to-hex-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!