如何转换特殊字符为十六进制? (Android中) [英] How to convert special characters to hex? ( in Android )

查看:215
本文介绍了如何转换特殊字符为十六进制? (Android中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

转换一些特殊字符转换为十六进制值。


  
  

例如:


  
  

的ㅂ十六进制值是85 E3 82


  
  

的ㅈ的十六进制值,ㄷ和ㄱ是E3 85 88,E3 84的b7,并分别E3 84 B1


我试过下面的方法,但它仅适用于;,#等。


  

Integer.toHexString(ㅂ),使3142值。但正确的十六进制值应该是E3 85 82。


  
  

字符串为十六进制



解决方案

您的答案编码是在UNI code(十六进制),你需要将其转换成UTF8(十六进制)

字符串转换为十六进制。

 公共静态无效的主要(字串[] args)抛出UnsupportedEncodingException {
    字符串CHR =ㅂ;
    System.out.print(toHex(CHR));
}
//字符串为十六进制
公共静态字符串toHex(字符串ARG)抛出UnsupportedEncodingException {
    根据您的需要//更改编码
    返回的String.format(%04X,新的BigInteger(1,arg.getBytes(UTF8)));
}

输出: - e38582

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

解决方案

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

这篇关于如何转换特殊字符为十六进制? (Android中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆