Java:字符串到字节数组的转换

Java:字符串到字节数组的转换,java,serialization,type-conversion,marshalling,Java,Serialization,Type Conversion,Marshalling,我从一个简单的测试中得到了一些意想不到的结果。运行以下命令后: byte [] bytes = {(byte)0x40, (byte)0xE2, (byte)0x56, (byte)0xFF, (byte)0xAD, (byte)0xDC}; String s = new String(bytes, Charset.forName("UTF-8")); byte[] bytes2 = s.getBytes(Charset.forName("UTF-8")); bytes2是一个14元素长的数组

我从一个简单的测试中得到了一些意想不到的结果。运行以下命令后:

byte [] bytes = {(byte)0x40, (byte)0xE2, (byte)0x56, (byte)0xFF, (byte)0xAD, (byte)0xDC};
String s = new String(bytes, Charset.forName("UTF-8"));
byte[] bytes2 = s.getBytes(Charset.forName("UTF-8"));
bytes2是一个14元素长的数组,与原始字节完全不同。有没有办法进行这种转换并保留原始的字节分解

有没有办法进行这种转换并保留原始的字节分解

对我来说,这看起来不像是有效的UTF-8,所以我并不奇怪它不是往返的

如果要以可逆方式将任意二进制数据转换为文本,请使用base64,例如via

有没有办法进行这种转换并保留原始的字节分解

对我来说,这看起来不像是有效的UTF-8,所以我并不奇怪它不是往返的

如果要以可逆方式将任意二进制数据转换为文本,请使用base64,例如通过。

两件事:

字节序列似乎不是有效的UTF-8

 $ python
 >>> '\x40\xe2\x56\xff\xad\xdc'.decode('utf8')
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
     return codecs.utf_8_decode(input, errors, True)
 UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 1: invalid continuation byte
即使它是有效的UTF-8,由于预组合字符和其他Unicode特性,解码和编码可能会产生不同的字节

如果您想将任意二进制数据编码为字符串,并保证在解码时返回相同的字节,那么最好的选择是base64

有两件事:

字节序列似乎不是有效的UTF-8

 $ python
 >>> '\x40\xe2\x56\xff\xad\xdc'.decode('utf8')
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
     return codecs.utf_8_decode(input, errors, True)
 UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 1: invalid continuation byte
即使它是有效的UTF-8,由于预组合字符和其他Unicode特性,解码和编码可能会产生不同的字节

如果您想将任意二进制数据编码为字符串,并保证在解码时返回相同的字节,那么最好的选择是base64

这应该可以:

public class Main
{

    /*
     * This method converts a String to an array of bytes
     */
    public void convertStringToByteArray()
    {

        String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";

        byte[] theByteArray = stringToConvert.getBytes();

        System.out.println(theByteArray.length);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {    
        new Main().convertStringToByteArray();
    }
}
这应该做到:

public class Main
{

    /*
     * This method converts a String to an array of bytes
     */
    public void convertStringToByteArray()
    {

        String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";

        byte[] theByteArray = stringToConvert.getBytes();

        System.out.println(theByteArray.length);

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {    
        new Main().convertStringToByteArray();
    }
}

一般来说,你说bytes2与原文完全不同-将其包含在问题中仍然是有用的。一般来说,你说bytes2与原文完全不同-将其包含在问题中仍然是有用的。飞碟一定是这样。此方法始终使用此字符集的默认替换字节数组替换格式错误的输入和不可映射的字符序列。为了检测这样的序列,直接使用CharsetDecoder.decodejava.nio.ByteBuffer方法。飞碟一定是这样。此方法始终使用此字符集的默认替换字节数组替换格式错误的输入和不可映射的字符序列。为了检测这样的序列,直接使用CharsetDecoder.decodejava.nio.ByteBuffer方法。