中文和英文对应的字节
生活随笔
收集整理的這篇文章主要介紹了
中文和英文对应的字节
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
中文對應的字節
public class ByteBitDemo {public static void main(String[] args) throws Exception{String a = "尚";byte[] bytes = a.getBytes();for (byte b : bytes) {System.out.print(b + " ");String s = Integer.toBinaryString(b);System.out.println(s);}}}運行程序:我們發現一個中文是有 3 個字節組成
我們修改 編碼格式 , 編碼格式改成 GBK ,我們在運行發現變成了 2 個字節
public static void main(String[] args) throws Exception{String a = "尚";// 在中文情況下,不同的編碼格式,對應不同的字節//GBK :編碼格式占2個字節// UTF-8:編碼格式占3個字節byte[] bytes = a.getBytes("GBK");// byte[] bytes = a.getBytes("UTF-8");for (byte b : bytes) {System.out.print(b + " ");String s = Integer.toBinaryString(b);System.out.println(s);} }運行程序
英文對應的字節
我們在看看英文,在不同的編碼格式占用多少字節
public class ByteBit {public static void main(String[] args) throws Exception{String a = "A";byte[] bytes = a.getBytes();// 在中文情況下,不同的編碼格式,對應不同的字節 // byte[] bytes = a.getBytes("GBK");for (byte b : bytes) {System.out.print(b + " ");String s = Integer.toBinaryString(b);System.out.println(s);}} }運行程序
?
總結
以上是生活随笔為你收集整理的中文和英文对应的字节的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: byte和bit的关系
- 下一篇: 对称加密的原理