如何將Java String轉換爲byte []?

本文翻譯自:How to convert Java String into byte[]?

Is there any way to convert Java String to a byte[] ( not the boxed Byte[] )? 有沒有辦法將Java String轉換爲byte[]不是盒裝的Byte[] )?

In trying this: 在嘗試這個:

System.out.println(response.split("\r\n\r\n")[1]);
System.out.println("******");
System.out.println(response.split("\r\n\r\n")[1].getBytes().toString());

and I'm getting separate outputs. 而我正在獲得單獨的輸出。 Unable to display 1st output as it is a gzip string. 無法顯示第一個輸出,因爲它是一個gzip字符串。

<A Gzip String>
******
[B@38ee9f13

The second is an address. 第二個是地址。 Is there anything I'm doing wrong? 有什麼我做錯了嗎? I need the result in a byte[] to feed it to gzip decompressor, which is as follows. 我需要在byte[]中將結果提供給gzip decompressor,如下所示。

String decompressGZIP(byte[] gzip) throws IOException {
    java.util.zip.Inflater inf = new java.util.zip.Inflater();
    java.io.ByteArrayInputStream bytein = new java.io.ByteArrayInputStream(gzip);
    java.util.zip.GZIPInputStream gzin = new java.util.zip.GZIPInputStream(bytein);
    java.io.ByteArrayOutputStream byteout = new java.io.ByteArrayOutputStream();
    int res = 0;
    byte buf[] = new byte[1024];
    while (res >= 0) {
        res = gzin.read(buf, 0, buf.length);
        if (res > 0) {
            byteout.write(buf, 0, res);
        }
    }
    byte uncompressed[] = byteout.toByteArray();
    return (uncompressed.toString());
}

#1樓

參考:https://stackoom.com/question/1FvDr/如何將Java-String轉換爲byte


#2樓

  String example = "Convert Java String";
  byte[] bytes = example.getBytes();

#3樓

The object your method decompressGZIP() needs is a byte[] . 您的方法decompressGZIP()需要的對象是byte[]

So the basic, technical answer to the question you have asked is: 因此,您提出的問題的基本技術答案是:

byte[] b = string.getBytes();
byte[] b = string.getBytes(Charset.forName("UTF-8"));
byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only

However the problem you appear to be wrestling with is that this doesn't display very well. 然而,你似乎正在努力解決的問題是,這並不能很好地顯示出來。 Calling toString() will just give you the default Object.toString() which is the class name + memory address. 調用toString()只會給你默認的Object.toString() ,這是類名+內存地址。 In your result [B@38ee9f13 , the [B means byte[] and 38ee9f13 is the memory address, separated by an @ . 在你的結果[B@38ee9f13[B表示byte[]38ee9f13是內存地址,用@分隔。

For display purposes you can use: 出於顯示目的,您可以使用:

Arrays.toString(bytes);

But this will just display as a sequence of comma-separated integers, which may or may not be what you want. 但這隻會顯示爲逗號分隔的整數序列,這可能是您想要的也可能不是。

To get a readable String back from a byte[] , use: 要從byte[]獲取可讀的String ,請使用:

String string = new String(byte[] bytes, Charset charset);

The reason the Charset version is favoured, is that all String objects in Java are stored internally as UTF-16. Charset版本受青睞的原因是Java中的所有String對象都在內部存儲爲UTF-16。 When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String , depending upon the chosen charset. 轉換爲byte[]您將獲得該String的給定字形的不同字節細分,具體取決於所選的字符集。


#4樓

您可以使用String.getBytes()返回byte[]數組。


#5樓

Try using String.getBytes(). 嘗試使用String.getBytes()。 It returns a byte[] representing string data. 它返回表示字符串數據的byte []。 Example: 例:

String data = "sample data";
byte[] byteData = data.getBytes();

#6樓

Simply: 只是:

String abc="abcdefghight";

byte[] b = abc.getBytes();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章