如何实现把十六进制字符串转换成byte[]
也就是要把"ABBA"转换成{10101011,10111010}

解决方案 »

  1.   

    好像得自己写
    没有api级的
      

  2.   

    BigInteger("abba", 16).toString(2);
      

  3.   

    s=Integer.toBinaryString(Integer.parseInt("abba",16));
      

  4.   

    不对,我要取得的是
    byte[0] = 10101011
    byte[1] = 10111010
      

  5.   

    楼主您理解的byte是byte[0] = 10101011,byte[1] = 10111010这种形式吗?那我帮不了你了,我做的byte是这样形式,byte[0]='0'。
      

  6.   

    自己写了一个,不知道有没有简单的方法
         if(hexString.length()%2 != 0)
         throw new Exception("Hex字符串长度不正确");
         int length = hexString.length()/2;
         byte[] newBytes = new byte[length];    
         for(int i=0;i<length;i++)
         {
         String str = hexString.substring(i*2,(i+1)*2);
         newBytes[i] = (byte)Integer.parseInt(str,16);
         }
        
         return newBytes;
      

  7.   

    public class Test {  public static final int BytesToInt(int b[]) throws Exception {
        if (b.length != 4)
          throw new Exception("invalid length");
        int result = 0;
        for (int i = 0; i < 4; i++) {
          result = ( (result << 4) | Integer.parseInt(String.valueOf(b[i]),2));
        }
        return result;
      }
      public static void main(String args[]) {
        try {
          int[] val = {0, 00, 0001, 1001};
          System.out.println(Test.BytesToInt(val));
        }
        catch (Exception ex) {
        }
      }
    }