Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将ASCII控制字符替换为Unicode符号作为控制字符_Java_Android_Unicode_Character Encoding - Fatal编程技术网

Java 将ASCII控制字符替换为Unicode符号作为控制字符

Java 将ASCII控制字符替换为Unicode符号作为控制字符,java,android,unicode,character-encoding,Java,Android,Unicode,Character Encoding,我正在寻找一种快速简便的方法,用适当的unicode符号替换Ascii控制字符,以便于记录 示例: ASCII码→ 期望输出 0x00→ ␀ (U+2400) 0x01→ ␁ (U+2401) 0x02→ ␂ (U+2402) 0x1F→ ␟ (U+241F) 我的输入是一个长度已知的字节数组。我目前使用新字符串(byData,0,nLength,StandardCharsets.US\u ASCII).replaceAll(“\\W,”)�");但这会删除诸如回车之类的有用信息(␍). 我

我正在寻找一种快速简便的方法,用适当的unicode符号替换Ascii控制字符,以便于记录

示例:

  • ASCII码→ 期望输出
  • 0x00→ ␀ (U+2400)
  • 0x01→ ␁ (U+2401)
  • 0x02→ ␂ (U+2402)
  • 0x1F→ ␟ (U+241F)
我的输入是一个长度已知的字节数组。我目前使用
新字符串(byData,0,nLength,StandardCharsets.US\u ASCII).replaceAll(“\\W,”)�");但这会删除诸如回车之类的有用信息(␍).

我知道我可以手动查找并替换32个控制字符中的每一个,但我认为必须有更好更快的方法

我的项目安装了番石榴,所以如果有一些番石榴魔术,让我知道


我正在使用Java 7/Android。

手动循环可能是最好的选择:

String s = new String(byData, 0, nLength, StandardCharsets.US_ASCII);
StringBuilder sb = new StringBuilder(s);

for (int i = 0; i < sb.length(); i++) {
    int ch = (int) sb.charAt(i);
    if ((cp < 32) && (ch != 9) && (ch != 10) && (ch != 13)) {
        sb.setCharAt(i, (char)(0x2400 + ch));
    }
}

s = sb.toString();
String s=新字符串(byData,0,nLength,StandardCharsets.US\u ASCII);
StringBuilder sb=新的StringBuilder;
for(int i=0;i
这很有效。您介意我编辑您的代码以匹配我最终编写的实用程序方法吗?