首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Java中将十六进制转换为ANSI (窗口1252)并将ANSI (窗口1252)转换回十六进制字符串?

如何在Java中将十六进制转换为ANSI (窗口1252)并将ANSI (窗口1252)转换回十六进制字符串?
EN

Stack Overflow用户
提问于 2019-05-06 07:31:41
回答 1查看 509关注 0票数 0

如何在Java中将十六进制字符串转换为ansi (窗口1252)和ansi (窗口1252)为十六进制字符串。

python (工作得很好)

代码语言:javascript
运行
复制
q = "hex string value"

x = bytes.fromhex(q).decode('ANSI')

a = x.encode("ANSI")
a = a.hex()
if q==a:
    print("Correct")

Java (这段代码有问题)

代码语言:javascript
运行
复制
String hexOri = "hex string value";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hexOri.length(); i+=2) {
    String str = hexOri.substring(i, i+2);
    output.append((char)Integer.parseInt(str, 16));
}
System.out.println("ANSI = " + output);
char [] chars = output.toString().toCharArray();
StringBuffer hexOutput = new StringBuffer();

for(int i = 0; i < chars.length; i++){
  hexOutput.append(Integer.toHexString((int)chars[i]));
}
System.out.println("HexOutput = " + hexOutput.toString());
System.out.println(hexOri.equals(hexOutput.toString()));

来自Python的输出

对,是这样

Python的预期输出

对,是这样

来自Java的输出

错误

Java的预期输出

对,是这样

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-06 08:24:41

在java中,字符串是用UTF-16编码的,所以您不能简单地读取/写入字符串的字节来获得您想要的编码表示。

您应该使用String#getBytes(String str, String charset)将所需的编码转换成字符串,并将其序列化为字节数组。

使用new String(buffer,encoding)解码字节数组必须做同样的事情。

在这两种情况下,如果您使用没有字符集的方法,它将使用JVM实例的默认编码(应该是系统字符集)。

代码语言:javascript
运行
复制
    public static void main(String[] args) {
        String str = "\tSome text [à]";

        try {
            System.out.println(str); //     Some text [à]

            String windowsLatin1 = "Cp1252";
            String hexString = toHex(windowsLatin1, str);

            System.out.println(hexString); // 09536f6d652074657874205be05d

            String winString = toString(windowsLatin1, hexString);

            System.out.println(winString); //   Some text [à]
        } catch (UnsupportedEncodingException e) {
            // Should not happen.
        }

    }

    public static String toString(String encoding, String hexString) throws UnsupportedEncodingException {
        int length = hexString.length();
        byte [] buffer = new byte[length/2];
        for (int i = 0; i < length ; i+=2) {
            String hexVal = hexString.substring(i,i+2);
            byte code = (byte) Integer.parseInt(hexVal,16);
            buffer[i/2]=code;
        }
        String winString = new String(buffer,encoding);
        return winString;
    }

    public static String toHex(String encoding, String str) throws UnsupportedEncodingException {
        byte[] bytes = str.getBytes(encoding);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            String hexChar = Integer.toHexString(b & 0xff);
            if(hexChar.length()<2) {
                builder.append('0');
            }
            builder.append(hexChar);
        }
        String hexString = builder.toString(); // 09536f6d652074657874205be05d
        return hexString;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56000637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档