package fr.zng.xxzx.common.util;

import fr.zng.xxzx.util.ConvertUtil;	

public class CrcCode
{
	public static void main(String args[])
	{	//B8C3
//		long lon = GetModBusCRC("7F BE 6C 6D 09 DF 09 A8 09 FB 00 00 00 01 37 02 A5 02 75 02 18 00 46 00 9F 00 99 00 19 00 1A 00 0F 03 AD 03 DA 03 E3 00 46 00 9F 00 99 00 19 00 1A 00 0F 00"
//				.replace(" ", ""));
	    System.out.println(getCrcCode("000102030405060708091011"));
	}
	
	   public static String getCrcCode(String str){
	        str = str.replace(" ", "");
	        long lon = GetModBusCRC(str
	            .replace(" ", ""));
	        int h1, l0;
	        l0 = (int) lon / 256;
	        h1 = (int) lon % 256;
	        String s = "";
	        if (Integer.toHexString(h1).length() < 2)
	        {
	            s = "0" + Integer.toHexString(h1);
	        } else
	        {
	            s = Integer.toHexString(h1);
	        }
	        if (Integer.toHexString(l0).length() < 2)
	        {
	            s = s + "0" + Integer.toHexString(l0);
	        } else
	        {
	            s = s + Integer.toHexString(l0);
	        }
	            s = ConvertUtil.toFZDA(s);
	        return s;
	    }

	public static boolean checkCrcCode(String recData){
	    recData = recData.replace(" ", "");
	    int len = recData.length();
        String str = recData.substring(2, len - 4);
	    long lon = GetModBusCRC(str
            .replace(" ", ""));
        int h1, l0;
        l0 = (int) lon / 256;
        h1 = (int) lon % 256;
        String s = "";
        if (Integer.toHexString(h1).length() < 2)
        {
            s = "0" + Integer.toHexString(h1);
        } else
        {
            s = Integer.toHexString(h1);
        }
        if (Integer.toHexString(l0).length() < 2)
        {
            s = s + "0" + Integer.toHexString(l0);
        } else
        {
            s = s + Integer.toHexString(l0);
        }
        
        if (Integer.parseInt(ConvertUtil.toFZDA(s),16) == Integer.parseInt(recData.substring(len - 4, len), 16)) {
            return true;
        }
        return false;
	}
	
	private static int[] strToToHexByte(String hexString)
	{
		hexString = hexString.replace(" ", "");
		// ���Ȳ���ż����ô������ӿո�

		if ((hexString.length() % 2) != 0)
		{
			hexString += " ";
		}

		// �������飬����Ϊ��ת���ַ��ȵ�һ�롣
		int[] returnBytes = new int[hexString.length() / 2];

		for (int i = 0; i < returnBytes.length; i++)
			//����Ϊʲô����ָ�����?
			returnBytes[i] =  (0xff & Integer.parseInt(hexString.substring(i * 2, i * 2 + 2), 16));
		return returnBytes;
	}

	public static long GetModBusCRC(String DATA)
	{
		long functionReturnValue = 0;
		long i = 0;

		long J = 0;
		int[] v = null;
//		byte[] d = null;
		//֮ǰ֮���Դ���,����Ϊ�е����ֱ���Ϊ�Ǹ�����.
		v = strToToHexByte(DATA);

		long CRC = 0;
		CRC = 0x0000L;
		for (i = 0; i <= (v).length - 1; i++)
		{ // 2.�ѵ�һ��8λ��������ݣ���ͨѶ��Ϣ֡�ĵ�һ���ֽڣ���16λ��CRC�Ĵ����ĵ�8λ����򣬰ѽ�����CRC�Ĵ�����
			CRC = (CRC / 256) * 256L + (CRC % 256L) ^ v[(int) i];
			for (J = 0; J <= 7; J++)
			{ // 3.��CRC�Ĵ�������������һλ������λ����0����λ����������λ��
				// 4.������λΪ0���ظ���3�����ٴ�����һλ����
				// ������λΪ1��CRC�Ĵ��������ʽA001��1010 0000 0000 0001���������
				// 5.�ظ�����3��4��ֱ������8�Σ��������8λ���ȫ�������˴��?
				long d0 = 0;
				d0 = CRC & 1L;
				CRC = CRC / 2;
				if (d0 == 1)
					CRC = CRC ^ 0xa001L;
			} // 6.�ظ�����2������5������ͨѶ��Ϣ֡��һ�ֽڵĴ��?
		} // 7.���õ���CRC�Ĵ������ݼ�Ϊ��CRC�롣
		CRC = CRC % 65536;
		functionReturnValue = CRC;
		return functionReturnValue;
	}

}
