中文佔字節數

原來中文的佔的字節數跟編碼有關。java

String chinese = "a中午";
		byte[] b = chinese.getBytes("GBK");
		byte[] d = chinese.getBytes("gb2312");
		byte[] c = chinese.getBytes("utf8");
		System.out.println("GBK:"+b.length);              // 5
		System.out.println("gb2312:"+d.length);           // 5
		System.out.println("UTF8:"+c.length);            // 7

 GBK 和 gb2312編碼下,一箇中文佔兩個字節。 utf8編碼的實際上是3個字節。編碼

相關文章
相關標籤/搜索