我必须将字节数组转换为Android中的字符串,但我的字节数组包含负值。

如果我将该字符串再次转换为字节数组,我得到的值与原始字节数组值不同。

我该怎么做才能得到正确的转换?我用来做转换的代码如下:

// Code to convert byte arr to str:
byte[] by_original = {0,1,-2,3,-4,-5,6};
String str1 = new String(by_original);
System.out.println("str1 >> "+str1);

// Code to convert str to byte arr:
byte[] by_new = str1.getBytes();
for(int i=0;i<by_new.length;i++) 
System.out.println("by1["+i+"] >> "+str1);

我被这个问题难住了。


当前回答

private static String toHexadecimal(byte[] digest){
        String hash = "";
    for(byte aux : digest) {
        int b = aux & 0xff;
        if (Integer.toHexString(b).length() == 1) hash += "0";
        hash += Integer.toHexString(b);
    }
    return hash;
}

其他回答

下面是一些将字节数组转换为字符串的方法。我已经测试过了,效果很好。

public String getStringFromByteArray(byte[] settingsData) {

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(settingsData);
    Reader reader = new BufferedReader(new InputStreamReader(byteArrayInputStream));
    StringBuilder sb = new StringBuilder();
    int byteChar;

    try {
        while((byteChar = reader.read()) != -1) {
            sb.append((char) byteChar);
        }
    }
    catch(IOException e) {
        e.printStackTrace();
    }

    return sb.toString();

}

public String getStringFromByteArray(byte[] settingsData) {

    StringBuilder sb = new StringBuilder();
    for(byte willBeChar: settingsData) {
        sb.append((char) willBeChar);
    }

    return sb.toString();

}

根本问题是(我认为)你在不知不觉中使用了一个字符集:

 bytes != encode(decode(bytes))

在某些情况下。UTF-8就是这样一个字符集的例子。具体来说,某些字节序列在UTF-8中不是有效的编码。如果UTF-8解码器遇到这些序列中的一个,它可能会丢弃违规字节或将它们解码为“没有这样的字符”的Unicode码点。当然,当您尝试将字符编码为字节时,结果将有所不同。

解决方案是:

明确你所使用的字符编码;即使用String构造函数和String。使用显式字符集的toByteArray方法。 为您的字节数据使用正确的字符集…或者选择一个(例如“Latin-1”,其中所有字节序列都映射到有效的Unicode字符。 如果你的字节(真的)是二进制数据,你希望能够通过“基于文本”的通道传输/接收它们,使用像Base64编码…就是为了这个目的设计的。


对于Java,最常见的字符集是Java .nio.charset. standardcharsets。如果您正在编码一个可以包含任何Unicode字符值的字符串,那么建议使用UTF-8编码(UTF_8)。

如果你想在Java中实现1:1的映射,那么你可以使用ISO Latin Alphabet No. 1——通常被称为“Latin 1”或简称为“Latin”(ISO_8859_1)。注意,Java中的Latin-1是Latin-1的IANA版本,它为所有可能的256个值分配字符,包括控制块C0和C1。这些是不可打印的:您不会在任何输出中看到它们。

从Java 8开始,Java包含Java .util。Base64用于Base64编码/解码。对于url安全编码,您可能需要使用Base64。getUrlEncoder代替标准编码器。自Android Oreo(8)以来,这个类也存在于Android中,API级别为26。

private static String toHexadecimal(byte[] digest){
        String hash = "";
    for(byte aux : digest) {
        int b = aux & 0xff;
        if (Integer.toHexString(b).length() == 1) hash += "0";
        hash += Integer.toHexString(b);
    }
    return hash;
}

我确实注意到了一些答案里没有的东西。可以将字节数组中的每个字节强制转换为字符,并将它们放入字符数组中。然后字符串是new string (cbuf),其中cbuf是char数组。要进行反向转换,需要循环将每个字符转换为字节,然后放入字节数组中,这个字节数组将与第一个字节数组相同。


public class StringByteArrTest {

    public static void main(String[] args) {
        // put whatever byte array here
        byte[] arr = new byte[] {-12, -100, -49, 100, -63, 0, -90};
        for (byte b: arr) System.out.println(b);
        // put data into this char array
        char[] cbuf = new char[arr.length];
        for (int i = 0; i < arr.length; i++) {
            cbuf[i] = (char) arr[i];
        }
        // this is the string
        String s = new String(cbuf);
        System.out.println(s);

        // converting back
        byte[] out = new byte[s.length()];
        for (int i = 0; i < s.length(); i++) {
            out[i] = (byte) s.charAt(i);
        }
        for (byte b: out) System.out.println(b);
    }

}

public class byteString {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        String msg = "Hello";
        byte[] buff = new byte[1024];
        buff = msg.getBytes("UTF-8");
        System.out.println(buff);
        String m = new String(buff);
        System.out.println(m);


    }

}