例如"a",如何转为二进制码,得答案立刻给分

解决方案 »

  1.   

    我来解释一下1楼的方法:
    使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
      

  2.   

    http://www.regexlab.com/zh/encoding.htm
      

  3.   

    public static String string2Hex (String s) {
        StringBuffer sb = new StringBuffer ();
        byte[] buff = s.getBytes ();
        for (int i = 0; i < buff.length; i ++) {
            String t = Integer.toString (buff [i], 16);
            if (t.length () > 2) t = t.substring (t.length () - 2, t.length ());
            else if (t.length () == 1) t = "0" + t;
            sb.append (t);
            sb.append (i != 0 && i % 16 == 0 ? '\n' : ' ');
        }
        return sb.toString ();
    }
      

  4.   

    byte[] s="a".getBytes();
    还可以转化为相应的字符集:
    byte[] s="a".getBytes("gb2312");接分!!!!!!!!
      

  5.   

    不好说,还是确定编码后再用
    String.getBytes();
      

  6.   

    byte[] getBytes() 
              使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。  byte[] getBytes(String charsetName) 
              使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。
      

  7.   

    例如 
      String str = "new world";
      byte[] myByte = new byte[str.length()];
      myByte = str.getBytes();
      
      int i = 0;
      
      while(i < str.length())
      {
      System.out.println(myByte[i]);
      i++;
      }
      

  8.   

    getBytes
    public byte[] getBytes(String charsetName)
                    throws UnsupportedEncodingException使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。 
    当此字符串不能在给定的字符集中解码时,该方法无指定的行为。当需要进一步控制解码过程时,应使用 CharsetEncoder 类。 
    参数:
    charsetName - 受支持的 charset 名称 
    返回:
    结果字节数组 
    抛出: 
    UnsupportedEncodingException - 如果指定的字符集不受支持
    从以下版本开始: 
    JDK1.1