繁体   English   中英

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

[英]Java: String to byte array conversion

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

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,例如通过此公共域编码器/解码器

应该这样做:

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

两件事情:

  1. 字节序列似乎无效的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 
  2. 即使它是有效的UTF-8,由于诸如预组合字符和其他Unicode功能之类的原因,解码然后进行编码也可能导致字节不同。

如果要以确保在解码时返回相同字节的方式在字符串中编码任意二进制数据,最好的选择是像base64。

暂无
暂无

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

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