Unicode转换为GB问题 String s="汉字";char ch=s.charAt(0);得到的ch是Unicode编码吧,如何把它转换成GB的编码? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package csdn;import java.io.UnsupportedEncodingException;public class toGBK { public static void main(String[] args) { try { String str = "汉字"; byte[] b = str.getBytes(); String toGBK = new String(b, "GBK"); System.out.println(toGBK); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } }} ByteBuffer encoded = Charset.forName("GBK").encode("汉字"); String s=URLEncoder.encode("中文", "UTF-8")URLEncoder.decode((s,"UTF-8") 在windows 环境下,String str = "汉字"; byte ch=str.getBytes()[0]; //它就是‘汉'的GBK第一个内码-70(0xba),同理str.getBytes()[1]...其它环境未测试,但你可以这样: public static void main(String[] args) { // TODO, add your application code String str = "汉字"; String s=null; try { s=new String(str.getBytes("GBK")); } catch(Exception e) { System.out.println(e); } byte[] ch=s.getBytes(); for(int i=0;i<4;i++) System.out.println(ch[i]); }结果为-70-70-41-42正对应“汉字”的GBK内码BA BA D7 D6 完整的代码:public class getgbk { public static void main(String[] args) { // TODO, add your application code String str = "汉字"; String s=null; try { s=new String(str.getBytes("GBK")); } catch(Exception e) { System.out.println(e); } byte[] b=s.getBytes(); for (int i = 0; i < b.length; i++) { String hex = Integer.toHexString(b[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } System.out.print(hex.toUpperCase() ); } 结果为BABAD7D6 to : lijielove520GB 就是国(家)标(准),汉字编码最早的为GB2312-80to:楼主 我的代码在cygwin(UTF-8)环境下测试没问题 但必须用s=new String(str.getBytes("GBK"));转换 String s = "汉字"; StringBuilder rs = new StringBuilder(); byte[] encoded = Charset.forName("GBK").encode(s).array(); for(byte b : encoded){ rs.append(String.format("%X", b & 0xff));} System.out.println(rs.toString()); break 标签,为什么总是出错。。。 如何在初始化时使用基类对象进行赋值 为什么这个程序放不出音乐来 求:JSP页面中下拉框的代码 Scanner输入问题 java能实现全屏显示吗? 本科毕业,一两年工作经验,熟悉J2EE,Oracle,Unix,英语四级,地点上海,这样的条件一般月薪能有多少? 继续刚才的 菜鸟基础问题2 JDBC for mysql driver 请教原代码: 诸位帮忙!急! jquery做的动态表格怎么提交到后台
package csdn;import java.io.UnsupportedEncodingException;public class toGBK {
public static void main(String[] args) {
try {
String str = "汉字";
byte[] b = str.getBytes();
String toGBK = new String(b, "GBK");
System.out.println(toGBK);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
String s=URLEncoder.encode("中文", "UTF-8")URLEncoder.decode((s,"UTF-8")
String str = "汉字";
byte ch=str.getBytes()[0]; //它就是‘汉'的GBK第一个内码-70(0xba),同理str.getBytes()[1]...
其它环境未测试,但你可以这样:
public static void main(String[] args) {
// TODO, add your application code
String str = "汉字";
String s=null;
try {
s=new String(str.getBytes("GBK"));
}
catch(Exception e) {
System.out.println(e);
}
byte[] ch=s.getBytes();
for(int i=0;i<4;i++)
System.out.println(ch[i]);
}
结果为
-70
-70
-41
-42
正对应“汉字”的GBK内码
BA BA D7 D6
public static void main(String[] args) {
// TODO, add your application code
String str = "汉字";
String s=null;
try {
s=new String(str.getBytes("GBK"));
}
catch(Exception e) {
System.out.println(e);
}
byte[] b=s.getBytes();
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
System.out.print(hex.toUpperCase() );
}
结果为
BABAD7D6
GB 就是国(家)标(准),汉字编码最早的为GB2312-80
to:楼主
我的代码在cygwin(UTF-8)环境下测试没问题
但必须用s=new String(str.getBytes("GBK"));转换
StringBuilder rs = new StringBuilder();
byte[] encoded = Charset.forName("GBK").encode(s).array();
for(byte b : encoded){ rs.append(String.format("%X", b & 0xff));}
System.out.println(rs.toString());