//一下代码测试通过
    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();
    }

解决方案 »

  1.   

    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
    不一样,我不知道那个对了?
      

  2.   

    String str = "123abc中国";   
     byte[] a=str.getBytes();
     for(int i=0;i<a.length ;i++){
        System.out.println(Integer.toHexString((256+a[i])%256)) ;
     }
      

  3.   

    补充结果:313233616263D6D0B9FA
      

  4.   

    to zmj1977(浪子) 
    你的代码跑起来的结果是这样的吧
    31323361626392868d91不是 补充结果:313233616263D6D0B9FA这个
      

  5.   

    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
      

  6.   

    to LOVEROSE(旺旺) 有问题吗???
      

  7.   

    To zmj1977(浪子) 
    是不是编码跟操作系统有关?
    我是日文版的win2000结果如下:
    31323361626392868D91
      

  8.   

    是的, 这主要根据jvm 来中的字符集来编码和解码,默认的jvm的字符集是由操作系统来决定。你最好用中文的字符集转化一下或者察看一下jvm的字符集是什么?
      

  9.   

    如果是用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 
      

  10.   

    先转换编码为ISO-8859-1,然后再进行toHexString
      

  11.   

    附送一个打印区位码表的小程序:
    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'>&nbsp;&nbsp;&nbsp;&nbsp;</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'>&nbsp;&nbsp;&nbsp;&nbsp;</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'>&nbsp;&nbsp;&nbsp;&nbsp;</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打开即可。
      

  12.   

    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();