简体   繁体   English

在 Java 中解码和替换字符串中的十六进制值

[英]Decode and replace hex values in a string in Java

I have the a string in Java which contains hex values beneath normal characters.我在 Java 中有一个字符串,它包含普通字符下方的十六进制值。 It looks something like this:它看起来像这样:

String s = "Hello\xF6\xE4\xFC\xD6\xC4\xDC\xDF"

What I want is to convert the hex values to the characters they represent, so it will look like this:我想要的是将十六进制值转换为它们代表的字符,所以它看起来像这样:

"HelloöäüÖÄÜß"

Is there a way to replace all hex values with the actual character they represent?有没有办法用它们代表的实际字符替换所有十六进制值?

I can achieve what I want with this, but I have to do one line for every character and it does not cover unexcepted characters:我可以用这个实现我想要的,但我必须为每个字符做一行,并且它不包括无例外的字符:

indexRequest = indexRequest.replace("\\xF6", "ö");
indexRequest = indexRequest.replace("\\xE4", "ä");
indexRequest = indexRequest.replace("\\xFC", "ü");
indexRequest = indexRequest.replace("\\xD6", "Ö");
indexRequest = indexRequest.replace("\\xC4", "Ä");
indexRequest = indexRequest.replace("\\xDC", "Ü");
indexRequest = indexRequest.replace("\\xDF", "ß");
public static void main(String[] args) {
    String s = "Hello\\xF6\\xE4\\xFC\\xD6\\xC4\\xDC\\xDF\\xFF ";

    StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile("\\\\x[0-9A-F]+");
    Matcher m = p.matcher(s);
    while(m.find()){           
        String hex = m.group();            //find hex values            
        int    num = Integer.parseInt(hex.replace("\\x", ""), 16);  //parse to int            
        char   bin = (char)num;            // cast int to char
        m.appendReplacement(sb, bin+"");   // replace hex with char         
    }
    m.appendTail(sb);
    System.out.println(sb.toString());
} 

I would loop through every chacter to find the '\\' and than skip one char and start a methode with the next two chars.我会遍历每个字符以找到“\\”,然后跳过一个字符并使用接下来的两个字符开始一个方法。 And than just use the code by Michael Berry here: Convert a String of Hex into ASCII in Java而不仅仅是在这里使用 Michael Berry 的代码: Convert a String of Hex into ASCII in Java

You can use a regex [xX][0-9a-fA-F]+ to identify all the hex code in your string, convert them to there corresponding character using Integer.parseInt(matcher.group().substring(1), 16) and replace them in string.您可以使用正则表达式[xX][0-9a-fA-F]+来识别字符串中的所有十六进制代码,使用Integer.parseInt(matcher.group().substring(1), 16)将它们转换为相应的字符Integer.parseInt(matcher.group().substring(1), 16)并将它们替换为字符串。 Below is a sample code for it下面是它的示例代码

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HexToCharacter {
public static void main(String[] args) {
    String s = "HelloxF6xE4xFCxD6xC4xDCxDF";
    StringBuilder sb = new StringBuilder(s);
    Pattern pattern = Pattern.compile("[xX][0-9a-fA-F]+");
    Matcher matcher = pattern.matcher(s);
    while(matcher.find()) {
        int indexOfHexCode = sb.indexOf(matcher.group());
        sb.replace(indexOfHexCode, indexOfHexCode+matcher.group().length(), Character.toString((char)Integer.parseInt(matcher.group().substring(1), 16)));
    }
    System.out.println(sb.toString());
}

} }

I have tested this regex pattern using your string.我已经使用您的字符串测试了这个正则表达式模式。 If there are other test-cases that you have in mind, then you might need to change regex accordingly如果您有其他测试用例,那么您可能需要相应地更改正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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