1. 程式人生 > >JavaSE8基礎 String getBytes 將不含中文的字符串轉換成字節數組

JavaSE8基礎 String getBytes 將不含中文的字符串轉換成字節數組

es2017 logs 字符 public res bsp clas 源碼 技術分享



os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)


code:

package jizuiku.t01;

import java.nio.charset.Charset;

public class Demo01 {
	public static void main(String[] args) {

		String str = "[email protected]#$";
		// 在字符串轉換成 字節數組,字節數組中存儲的是各個字符的ascii碼值
		byte[] b = str.getBytes();

		for (int i = 0, n = b.length; i < n; ++i) {
			System.out.print(b[i] + " ");
		}
		
		//為什麽強調 不含中文的字符串呢? 含有中文的話,就涉及到了字符集的問題。
	}
}


result:
技術分享


Java優秀,值得學習。
學習資源:API手冊+Java源碼。

JavaSE8基礎 String getBytes 將不含中文的字符串轉換成字節數組