//一下代码测试通过
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();
}
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();
}
{
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
不一样,我不知道那个对了?
byte[] a=str.getBytes();
for(int i=0;i<a.length ;i++){
System.out.println(Integer.toHexString((256+a[i])%256)) ;
}
你的代码跑起来的结果是这样的吧
31323361626392868d91不是 补充结果:313233616263D6D0B9FA这个
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
是不是编码跟操作系统有关?
我是日文版的win2000结果如下:
31323361626392868D91
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
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打开即可。
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();