Java 程式如何將字串轉換為位元組陣列


這是我們的字串。

String str = "Asia is a continent!";

現在讓我們使用位元組陣列和 getBytes() 方法達成我們的目標。

byte[] byteVal = str.getBytes();

現在,如果我們獲取陣列的長度,它將返回如下所示的完整示例中的長度 −

示例

 真實演示

public class Demo {
   public static void main(String args[]) {
      String str = "Asia is a continent!";
      System.out.println(str);
      // converted to byte array
      byte[] byteVal = str.getBytes();
      // getting the length
      System.out.println(byteVal.length);
   }
}

輸出

Asia is a continent!
20

更新日期: 2020-06-26

347 次瀏覽

開啟你的 職業生涯

透過完成課程取得認證

開始
廣告
© . All rights reserved.