简体   繁体   English

字节[]到字符[]

[英]Byte[] to char[]

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] 我需要以char形式显示原始字节数据,我遇到了一些问题,例如[1,32,-1,-1](字节数据)转换为char时出现为[1,3 ,2,-,1,--1]

My requirement 我的要求

[ 1, 32, -1, -1 ] in char format. [1,32,-1,-1]以char格式。

Intailly 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 提前致谢

If you just need a printable representation: 如果您只需要可打印的表示形式:

String readable = Arrays.toString(byte1);

But as stated you need a string Array like thing for this. 但是如上所述,您需要像这样的字符串数组。

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, 为什么需要坚持使用字符串,使用char数组,首先附加多余的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]);
    }
    }
}

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!\\-\0x7f\0xfb\0xe9

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 127 separately since I didn't want a DEL in the string - not sure what effect that would have depending on the environment. 我也单独捕获了字符值127因为我不想在字符串中使用DEL不知道根据环境会有什么影响。

If your requirement is indeed exactly as you describe, you might consider the following code (which produces exactly what you asked for): 如果确实符合您的描述要求,则可以考虑使用以下代码(产生的代码完全符合您的要求):

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);

Output: 输出:

[1,32,-1,-1]

Notice - since some numbers require more than one character, you end up with a string that is more than 4 characters long. 注意-由于某些数字需要多个字符,因此最终得到的字符串长度超过4个字符。 There is no way around that. 没有办法解决。

由于char字段的字符数为1个字符,因此无法在char字段中存储2个字符。

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

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