javaä¸çcharå 2个åè
1ï¼âåèâæ¯byteï¼âä½âæ¯bit ï¼
ãã2ï¼ 1 byte = 8 bit ï¼
ããchar å¨javaä¸æ¯2个åèãjavaéç¨unicodeï¼2个åèï¼16ä½ï¼æ¥è¡¨ç¤ºä¸ä¸ªå符ã
代ç å¦ä¸ï¼
public class Test {
public static void main(String[] args) {
String str= "ä¸";
char x ='ä¸';
byte[] bytes=null;
byte[] bytes1=null;
try {
bytes = str.getBytes("utf-8");
bytes1 = charToByte(x);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("bytes 大å°ï¼"+bytes.length);
System.out.println("bytes1大å°ï¼"+bytes1.length);
}
public static byte[] charToByte(char c) {
byte[] b = new byte[2];
b[0] = (byte) ((c & 0xFF00) >> 8);
b[1] = (byte) (c & 0xFF);
return b;
}
}
ç»æå¦ä¸ï¼
bytes 大å°ï¼3
bytes1大å°ï¼2
温馨提示:答案为网友推荐,仅供参考