天天看點

Java中 byte[] 數組與 String 字元串的轉化

1. String 轉 byte[]

String str = "Hello";//聲明一個字元串
byte[] srtbyte = str.getBytes();//使用string類的getBytes方法進行轉換
           

2. byte[] 轉 String

srtbyte = str.getBytes("UTF-8");//設定轉換的編碼格式
String res = new String(srtbyte,"UTF-8");