String address="00-15-f2-65-ec-59";
String[] split=address.split("\\-");
byte src[] = new byte[] { (byte) 0x00, (byte) 0x15,(byte) 0xf2, (byte) 0x65, (byte) 0xec,(byte)0x59 };如何把上面的split中的值转换成src中的值?即00→0x00,15→0x15

解决方案 »

  1.   

    你是说要把String类型转换成byte型吗?
    如果是的话,使用DecimalFormat类
    例:(希望可以帮到你)InputStreamReader isr=new InputStreamReader(System.in);//把读入字节流转为字符流.
    BufferedReader br=new BufferedReader(isr);//放入缓冲区
    String s=br.readLine();//读出该字符流.
    DecimalFormat df=new DecimalFormat();//创建一个DecimalFormat对象.
    Number n=df.parse(s);//调用df的parse方法,将S格式化为十进制数字,返回给n.
    byte b=n.byteValue();//调用Number对象n的byteValue方法,返回成为byte类型.
      

  2.   

    for (String str : split){
        System.out.println(Integer.parseInt(str, 16));
    }
      

  3.   

    参考:public static String Bytes2HexString(byte[] b)    //byte转换为十六进制
    {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
          String hex = Integer.toHexString(b[i]& 0xFF);
          if (hex.length() == 1) {
            hex = '0' + hex;
          }
          ret += hex.toUpperCase();
        }
        return ret;
    }