package com.device.comm.mylibrary.NativeDevice;

import java.io.UnsupportedEncodingException;

public class InvsUtil
{
  public InvsUtil() {}
  
  public static String bytesToHexString(byte[] src, int pos, int len)
  {
    StringBuilder stringBuilder = new StringBuilder("");
    if ((src == null) || (src.length <= 0)) {
      return null;
    }
    if (len > src.length) {
      len = src.length;
    }
    for (int i = pos; i < len; i++)
    {
      int v = src[i] & 0xFF;
      String hv = Integer.toHexString(v);
      if (hv.length() < 2) {
        stringBuilder.append(0);
      }
      stringBuilder.append(hv);
    }
    return stringBuilder.toString();
  }
  
  public static boolean checkHead(byte[] data)
  {
    byte[] head = { -86, -86, -86, -106, 105 };
    for (int i = 0; i < head.length; i++) {
      if (data[i] != head[i]) {
        return false;
      }
    }
    return true;
  }
  
  private static String byte2Ucs2(byte[] data, int pos, int len)
  {
    try
    {
      return new String(data, pos, len, "UTF-16LE").trim();
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException) {}
    return "";
  }
  
  public static InvsIdCard IdCardByte2String(byte[] data)
  {
    InvsIdCard info = new InvsIdCard();
    info.name = byte2Ucs2(data, 14, 30);
    info.sex = byte2Ucs2(data, 44, 2);
    switch (Integer.parseInt(info.sex))
    {
    case 0:
      info.sex = "未知";
      break;
    case 1:
      info.sex = "男";
      break;
    case 2:
      info.sex = "女";
      break;
    case 9:
      info.sex = "未知";
      break;
    }
    info.nation = byte2Ucs2(data, 46, 4);
    int nation = Integer.parseInt(info.nation);
    switch (nation)
    {
    case 94:
    case 95:
    case 96:
    case 97:
    case 98:
      info.nation = InvsConst.Nation1[(nation - 94)];
      break;
    default: 
      if ((nation < 1) || (nation > 56)) {
        nation = 0;
      }
      info.nation = InvsConst.Nation[nation];
    }
    info.birth = byte2Ucs2(data, 50, 16);
    info.address = byte2Ucs2(data, 66, 70);
    info.police = byte2Ucs2(data, 172, 30);
    info.start = byte2Ucs2(data, 202, 16);
    info.end = byte2Ucs2(data, 218, 16);
    
    byte[] arIdNo = new byte[18];
    if (info.idNo != null) {
      System.arraycopy(info.idNo.getBytes(), 0, arIdNo, 0, 18);
    }
    info.idNo = byte2Ucs2(data, 136, 36);
    
    System.arraycopy(data, 270, info.wlt, 0, 1024);
    
    return info;
  }
  
  public static byte ChkSum(byte[] data, int pos, int len)
  {
    if (len == 0) {
      return 0;
    }
    byte cSum = 0;
    int i = 0;
    while (i < len)
    {
      cSum = (byte)(cSum ^ data[(i + pos)]);
      i++;
    }
    return cSum;
  }
  
  public static byte[] CmdData(byte[] data)
  {
    byte[] buf = new byte[data.length + 8];
    buf[0] = -86;
    buf[1] = -86;
    buf[2] = -86;
    buf[3] = -106;
    buf[4] = 105;
    
    buf[5] = ((byte)(data.length + 1 >> 8 & 0xFF));
    buf[6] = ((byte)(data.length + 1 & 0xFF));
    
    System.arraycopy(data, 0, buf, 7, data.length);
    
    buf[(7 + data.length)] = ChkSum(buf, 5, data.length + 2);
    return buf;
  }
  
  public static byte[] getCmdData(int cmd, byte[] param)
  {
    switch (cmd)
    {
    case 4: 
      return CmdData(new byte[] { 32, 1 });
    case 5: 
      return CmdData(new byte[] { 32, 2 });
    case 22: 
      return CmdData(new byte[] { -80, 1 });
    case 6: 
      return CmdData(new byte[] { -96, 3 });
    case 15: 
      return CmdData(new byte[] { 48, 3 });
    case 23: 
      return CmdData(new byte[] { Byte.MIN_VALUE, 1 });
    case 8: 
      return CmdData(new byte[] { 80 });
    case 9: 
      return CmdData(new byte[] { 65 });
    case 11: 
      return CmdData(new byte[] { 64, 1 });
    case 12: 
      return CmdData(new byte[] { 64, 2 });
    case 13: 
      return CmdData(new byte[] { 64, 3 });
    case 21: 
      return CmdData(new byte[] { 67 });
    case 16: 
      return CmdData(new byte[] { -127, 1 });
    case 20: 
      byte[] data = new byte[param.length + 1];
      data[0] = 66;
      System.arraycopy(param, 0, data, 1, param.length);
      return CmdData(data);
    }
    return null;
  }
}


/* Location:              /Users/lvwang2002/Downloads/INVS300蓝牙开发包/INVS300蓝牙开发包/invs300_both(android蓝牙读卡演示软件源码20150810)/libs/invssdk.jar!/com/invs/InvsUtil.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */