如何将中文转换为其对应的十六进制字符串?(!!!高分!!!)

dyw 2003-10-21 10:19:53
如:
String str = "123abc中国";
// call convert function
String strHex = the_convert_function(str);
System.out.println("strHex: " + strHex);

以上片断中,the_convert_function函数是需要编写的转换函数。
该片断执行完之后,正确的结果应该是:
strHex: 313233616263D6D0B9FA

分析:
如果是英文数字,直接调用Integer.toHexString(...)即可,可是
其中的汉字该做何种处理?请编写the_convert_function函数。
...全文
295 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
LoveRose 2003-10-21
  • 打赏
  • 举报
回复
To zmj1977(浪子)
是不是编码跟操作系统有关?
我是日文版的win2000结果如下:
31323361626392868D91
zmj1977 2003-10-21
  • 打赏
  • 举报
回复
to LOVEROSE(旺旺) 有问题吗???
zmj1977 2003-10-21
  • 打赏
  • 举报
回复
String str = "123abc中国";
byte[] a=str.getBytes();
java.lang.StringBuffer hexStr=new java.lang.StringBuffer();
for(int i=0;i<a.length ;i++){
hexStr.append(Integer.toHexString((256+a[i])%256));

}
System.out.println(hexStr.toString().toUpperCase()) ;
}
display result:313233616263D6D0B9FA
pleonheart 2003-10-21
  • 打赏
  • 举报
回复
呵呵,这个题目蛮有趣的
LoveRose 2003-10-21
  • 打赏
  • 举报
回复
to zmj1977(浪子)
你的代码跑起来的结果是这样的吧
31323361626392868d91

不是 补充结果:313233616263D6D0B9FA这个
zmj1977 2003-10-21
  • 打赏
  • 举报
回复
补充结果:313233616263D6D0B9FA
zmj1977 2003-10-21
  • 打赏
  • 举报
回复
String str = "123abc中国";
byte[] a=str.getBytes();
for(int i=0;i<a.length ;i++){
System.out.println(Integer.toHexString((256+a[i])%256)) ;
}
caina3 2003-10-21
  • 打赏
  • 举报
回复
mark
LoveRose 2003-10-21
  • 打赏
  • 举报
回复
public static String the_convert_function(String str)
{
char[] chStr = str.toCharArray();
String result = new String();
for (int i = 0; i < chStr.length; i++)
{
System.out.println(chStr[i]);
result += Integer.toHexString(chStr[i]);
}

return result;
}

结果是:3132336162634e2d56fd
不一样,我不知道那个对了?
hzm1029 2003-10-21
  • 打赏
  • 举报
回复
//一下代码测试通过
String s = "123abc中国";
try {
byte[] b = s.getBytes();
String str = "";
for (int i = 0; i < b.length; i++) {
Integer I = new Integer(b[i]);
String strTmp = I.toHexString(b[i]);
if (strTmp.length() > 2)
strTmp = strTmp.substring(strTmp.length() - 2);
str = str + strTmp;

}
System.out.println(str.toUpperCase());
} catch (Exception e) {
e.printStackTrace();
}
qiyongjun2003 2003-10-21
  • 打赏
  • 举报
回复
建立一个对照表,然后检索。由于汉字编码没有规律可循。
exitzhang 2003-10-21
  • 打赏
  • 举报
回复
String hs="";
String stmp="";
for (int n=0;n<b.length;n++)
{
stmp=(java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length()==1) hs=hs+"0"+stmp;
else hs=hs+stmp;
if (n<b.length-1) hs=hs+":";
}
return hs.toUpperCase();
goldenhua 2003-10-21
  • 打赏
  • 举报
回复
附送一个打印区位码表的小程序:
class GB2312HTMLTable
{
public static void main(String[] args)
{
byte[] two_bytes = new byte[2];

System.out.print("<TABLE border='1' cellPadding=0 cellSpacing=0 width='199' style='border: 1px solid;' bordercolor='#ff6600'><tr>");
System.out.print("<TH bordercolordark=#ffffff bgColor=#FEC261 height='20' align='center'><font color='#000000'>    </font></TH>");
System.out.print("<TH bordercolordark=#ffffff bgColor=#FEC261 height='20' align='center'><font color='#000000'>位</font></TH>");

for(int i=1; i<=94; i++)
{
System.out.print("<TH bordercolordark=#ffffff bgColor=#FEC261 height='20' align='center'><font color='#000000'>" + i + "</font></TH>");
}

System.out.println("</tr>");
System.out.println("<tr><td style=border-style:'none'>区</td><td style=border-style:'none'>    </td><tr>");

for(int i=1; i<=94; i++)
{
for(int j=1; j<=94; j++)
{
if (j == 1)
{
System.out.print("<tr><td style=border-style:'none'>" + i + "</td><td style=border-style:'none'>    </td><td style=border-style:'none'>");
}
else
System.out.print("<td style=border-style:'none'>");

two_bytes[0] = (byte)(0xA0 + i);
two_bytes[1] = (byte)(0xA0 + j);
System.out.write(two_bytes, 0, 2);

System.out.print("</td>");
}

System.out.println("</tr>");

}
System.out.print("</table>");


}
}

编译后运行方式:
java GB2312HTMLTable >区位码表.htm

用IE打开即可。
xwlovesh 2003-10-21
  • 打赏
  • 举报
回复
先转换编码为ISO-8859-1,然后再进行toHexString
goldenhua 2003-10-21
  • 打赏
  • 举报
回复
如果是用Java来做,请注意你是需要Unicode的结果还是需要GB2312编码的结果或者需要区位码结果,处理方式略有区别。不过看你的需求"123abc中国"对应"313233616263D6D0B9FA"明显是GB2312编码。由于Java运行时String是采用Unicode编码的(如果在输入环境不存在错误转码的话),要得到GB2312编码,可以使用byte[] bytes = String.getBytes("GB2312")先得到GB2312编码的字节数组,然后把每个字节的二进制值转成16进制即可(这个不用写了吧)。

String.getBytes(charset)说明:
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException
Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.


Parameters:
charsetName - the name of a supported charset
Returns:
The resultant byte array
Throws:
UnsupportedEncodingException - If the named charset is not supported
Since:
JDK1.1
zmj1977 2003-10-21
  • 打赏
  • 举报
回复
是的, 这主要根据jvm 来中的字符集来编码和解码,默认的jvm的字符集是由操作系统来决定。你最好用中文的字符集转化一下或者察看一下jvm的字符集是什么?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧