java AlphaEncoder

public clas AlphaEncoder{
    private AlphaEncoder(){
    }
    public static String encode(byte[] b){
        int len = b.length;
        StringBufer sb = new StringBuffer(len<<1);
        for(int i=0;i<len;i++){
              sb.append((char)(((b[i]>>4)&0x0f)+'A'));
              sb.append((char)((b[i]&0x0f+'a'));
        }
        return sb.toString();
    }


   public static byte[] decode(String s){
       int len = s.length()>>1;
       byte[] b = new byte[len];
       for (int i=0,j=0;i<len;){
            int hi = s.charAt(j++)-'A';
            int lo = s.charAt(j++)-'a';
            if (hi<0 || hi>0x0f || lo<0 || lo>0x0f){
                throw new IllegalArgumentException(s);
            }
            b[i++] = (byte)(hi<<4)+lo);
       }
       return b;
   }
}

posted @ 2010-10-20 14:26  白羽雕弓  阅读(188)  评论(0编辑  收藏  举报