Java 字节[]到字符[]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20950424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:31:12  来源:igfitidea点击:

Byte[] to char[]

javacharbyte

提问by Jeetesh Nataraj

I am in a requirement of display raw byte data in char form, there are some issue I am stuck like for example [ 1, 32, -1, -1 ] ( byte data ) which when converted into char comes as [1, 3, 2, -, 1, - ,1]

我需要以字符形式显示原始字节数据,有一些问题我被卡住了,例如 [1, 32, -1, -1 ] (字节数据),当转换为字符时,它会变成 [1, 3 , 2, -, 1, - ,1]

My requirement

我的要求

[ 1, 32, -1, -1 ] in char format.

[ 1, 32, -1, -1 ] 字符格式。

Intailly

完全地

I used the command

我使用了命令

StringBuilder buffer = new StringBuilder();
    for(int i = 0; i < byte1.length;i++){
    buffer.append(byte1[i]);
    }
char[] c = buffer.toString().toCharArray();

Didn't serve my requirement.

没有满足我的要求。

Then I used

然后我用

char dig = (char)(((int)'0')+ (-32));

it gave an invalid output

Kindly help in my requirement. Thanks in advance

请帮助我的要求。提前致谢

采纳答案by Rorschach

Why you need to stick to strings, use array of char array, first append extra 0 in case its short and If your data is in byte format, use bytes1.toString()and then something like this,

为什么你需要坚持使用字符串,使用字符数组,首先附加额外的 0 以防它很短,如果你的数据是字节格式,使用bytes1.toString()然后像这样,

public class Example {

    public static void main(String args[])
    {
        String[] byte1 = {"2","45","56","1"};
        for (int i = 0; i < byte1.length; i++) {
            if(byte1[i].length()<2){
                byte1[i] = "0"+byte1[i];
            }
        }
        char[][] chr = new char[5][10];
        StringBuilder buffer = new StringBuilder();
        for(int i = 0; i < byte1.length;i++){
               chr[i] = byte1[i].toCharArray();
        }


    for (int i = 0; i < chr.length; i++) {
            System.out.println(chr[i]);
    }
    }
}

回答by SaggingRufus

Since a char field is litteraly 1 character, there is no way to store 2 characters in a char field.

由于 char 字段几乎是 1 个字符,因此无法在 char 字段中存储 2 个字符。

回答by Jan

If you just need a printable representation:

如果您只需要可打印的表示:

String readable = Arrays.toString(byte1);

But as stated you need a string Array like thing for this.

但是如前所述,您需要一个类似字符串数组的东西。

回答by Floris

Perhaps the following will work for you:

也许以下对你有用:

byte byte1[] = {72, 101, 108, 108, 111, 33, 92, 45, 127, -5, -23};
StringBuilder buffer = new StringBuilder();
    for(int i = 0; i < byte1.length;i++){
      if (byte1[i] >= 32 && byte1[i] != 92 && byte1[i] != 127) buffer.append((char)byte1[i]);
      else {
        String temp;
        if (byte1[i] == 92) {
          buffer.append("\\");
        }
        else {
          temp = String.format("\0x%02x", byte1[i]);
          buffer.append(temp);
        }
      }
    }
System.out.println(buffer);

This will produce the output:

这将产生输出:

Hello!\-
byte byte1[] = {1, 32, -1, -1};
StringBuilder buffer = new StringBuilder();
buffer.append("[");
for(int i = 0; i < byte1.length - 1;i++){
  buffer.append(String.format("%d,", byte1[i]));
}
buffer.append(String.format("%d]", byte1[byte1.length - 1]));
System.out.println(buffer);
x7f
[1,32,-1,-1]
xfb##代码##xe9

The "representable" characters will be printed normally, the others are turned into a "escaped" hex code. The \gets special treatment - if you want to show hex codes with an escape backslash, then you need a backslash to escape a backslash (and to get \\in the output, you need "\\\\"in the code...

“可表示”字符将正常打印,其他字符将转换为“转义”十六进制代码。将\得到特殊待遇-如果你想显示的十六进制代码的使用转义反斜杠,那么你需要一个反斜杠来转义反斜杠(并获得\\输出,你需要"\\\\"在代码中...

I also trapped the character value 127separately since I didn't want a DELin the string - not sure what effect that would have depending on the environment.

我还127单独捕获了字符值,因为我不想要DEL字符串中的 a - 不确定这会产生什么影响,具体取决于环境。

If your requirement is indeed exactly as you describe, you might consider the following code (which produces exactly what you asked for):

如果您的要求确实与您描述的完全一样,您可以考虑以下代码(它产生的正是您所要求的):

##代码##

Output:

输出:

##代码##

Notice - since some numbers require more than one character, you end up with a string that is more than 4 characters long. There is no way around that.

请注意 - 由于某些数字需要多个字符,因此您最终会得到一个长度超过 4 个字符的字符串。没有办法解决这个问题。