stringcharbyteslength

来源:互联网 发布:linux删除多个文件命令 编辑:程序博客网 时间:2024/06/05 15:45

package TestDemo.test;

import java.io.UnsupportedEncodingException;

public class TestStrByte{
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){e.printStackTrace();}
System.out.println(bytes.length);
System.out.println(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;
}
}

原创粉丝点击