简体   繁体   English

如何在Java中将字节数组转换为String,并跳过所有空字节?

[英]How to convert a byte array to a String in Java, and skip all null bytes?

I have a byte array that's contains a string at the end of the array, and the beginning of the array is padded with zeroes. 我有一个字节数组,在数组末尾包含一个字符串,并且数组的开头填充了零。 I'm using the following code to convert it to a string: 我正在使用以下代码将其转换为字符串:

String myText = new String(byteArray, "UTF-8");

However, I'm getting a bunch of weird characters prepended to the string, due to the 0 padding. 但是,由于填充为0,我在字符串前得到了一堆奇怪的字符。 How do I get rid of it? 我如何摆脱它?

Thanks. 谢谢。

Use the String(byte[], int, int, String) constructor. 使用String(byte[], int, int, String)构造函数。

The first int is an offset through the byte[] : just look for the first non-zero byte; 第一个intbyte[]的偏移量:只查找第一个非零字节; the second int is the number of bytes. 第二个int是字节数。 So, call like: 因此,呼叫方式如下:

new String(
    byteArray, firstNonNullByte, byteArray.length - firstNonNullByte, "UTF-8");

您可以使用apache org.apache.commons.lang3.ArrayUtils。

int firstNonNullByte = ArrayUtils.lastIndexOf(byteArray, 0) + 1;

I would try to remove the leading zeroes and then just use the remaining part of the byte array that is useful: 我会尝试删除前导零,然后仅使用有用的byte数组的其余部分:

public class Test {

 public static byte[] removeZeroes(byte[] data) {
    int i;
    for(i = 0; i < data.length; i++) {
        if(data[i] != '\0') {
            break;
        }
    }
    return Arrays.copyOfRange(data, i, data.length);
}

public static void main(String args[]) {
    byte[] byteArray = new byte[10];
    byteArray[0] = '\0';
    byteArray[1] = '\0';
    byteArray[2] = '\0';
    byteArray[3] = '\0';
    byteArray[4] = 's';
    byteArray[5] = 't';
    byteArray[6] = 'r';
    byteArray[7] = 'i';
    byteArray[8] = 'n';
    byteArray[9] = 'g';
    byteArray = removeZeroes(byteArray);

    try {
        String myText = new String(byteArray, "UTF-8");
        System.out.println(myText);
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

}

My solution would be to remove zeros from the beginning of the array: 我的解决方案是从数组的开头删除零:

public byte[] trim(byte[] bytes) {
        int i = 0;
        while (i<bytes.length && bytes[i] == 0) {
            i++;
        }

        return Arrays.copyOfRange(bytes, i, bytes.length);        
 }

No need to loop to find where the padding ends, you can fix the string using regex. 无需循环查找填充的结束位置,即可使用正则表达式修复字符串。 Index juggling with loops is dangerous, because it would a perfect place to introduce by-one error one day. 使用循环处理索引很危险,因为它是一天引入一个错误的理想之地。

String myText = (new String(byteArray, "UTF-8")).replaceAll("^\\x00*", "");

Regex means: 正则表达式是指:

  • at the beginning of string ( ^ ) 在字符串( ^ )的开头
  • character with hexadecimal code 0 ( \\x00 , and \\ should be escaped in java, so \\\\x00 ) 十六进制代码为0的字符( \\x00\\应该在Java中转义,因此\\\\x00
  • zero or more times ( * ) 零次或多次( *

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

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