就如把一个GUID“ 2c4dbc35-7985-42b1-a391-019e3acfea05”的存放到一个byte[16]的字节中。
然后如何把一个byte[16]的GUID反编译出来。

解决方案 »

  1.   

    用正则生成的试下
        String s="2c4dbc35-7985-42b1-a391-019e3acfea05";
         boolean match = s.matches("[[a-f][0-9][A-F]]{8}(-[[a-f][0-9][A-F]]{4}){3}-[[a-f][0-9][A-F]]{12}");
         if(match)//检查格式正确
         {
           byte[] data=new byte[16];
           
           //生成数组
            Pattern p = Pattern.compile("([[a-f][0-9][A-F]]{2})-?");
           String str = "2c4dbc35-7985-42b1-a391-019e3acfea05";
           Matcher m = p.matcher(str);
           int inx=0;
           while (m.find()) {
             for(int i=1;i<=m.groupCount();i++)
             {  
               data[inx++]=(byte)Integer.parseInt(m.group(i),16);
               System.out.println(m.group(i)) ;
             }
           }
           
           //还原数组
           inx=0; str="";
           for(int i=0;i<4;i++)      
             str=str+Integer.toHexString(data[inx++]&0xFF);
           for(int j=0;j<3;j++)
           {  
             str=str+"-";
             for(int i=0;i<2;i++)      
               str=str+Integer.toHexString(data[inx++]&0xFF);
           }
           for(int i=0;i<6;i++)      
             str=str+Integer.toHexString(data[inx++]&0xFF);
           System.out.println(str) ; 
         }