---
title: "北航一卡通的内容解析函数"
author: "hermitte"
date: "2007-02-01T23:02:04Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-02-01T23:02:04Z"
id: 51684
url: "https://hermitte.iteye.com/blog/51684"
---

北航一卡通的内容解析函数

using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Runtime.InteropServices;  
namespace RF.Util  
{

    class Rf  
    {

        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_link\_com(int comid);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_unlink\_com();  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_card();  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_load\_key(int sect, char\[] pass);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_rd\_keya\_card(int id18, int id0, int block, int offset, byte\[] content);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_beep(int keep);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_rd\_card(int id18, int id0, int block, int offset, ref string content);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_rd\_cardc(int id18, int id0, int block, int offset, ref string content);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_wr\_card(int id18, int id0, int block, int offset, ref string content);  
        \[DllImport("Rfdll32.dll")]  
        public static unsafe extern int rf\_wr\_cardc(int id18, int id0, int block, int offset, ref string content);

        //public unsafe string returncode()  
        //{  
        //    string a = rf\_link\_com(1) + "";  
        //    byte\[] content = new byte\[17];  
        //    rf\_rd\_keya\_card(18, 0, 2, content);

        //    return a;

        //}  
        public unsafe bool init()  
        {  
            Config config = Config.GetInstance();  
   
            //  a = rf\_link\_com(1);  
            int comPort = rf\_link\_com(config.ComID);  
            int keyAload = rf\_load\_key(0, "FFFFFFFFFFFF".ToCharArray());

            //rf\_beep(1);

            return true;  
        }  
        public unsafe void Beep() {  
             
            rf\_beep(1);  
        }

 

        public unsafe string readNum()  
        {  
            string CardValue = "";  
            string CardBh = "";

            byte\[] codeOrig = new byte\[32];  
            int codeN = 0;  
            bool isTeacher = false;  
            byte\[] testCode = new byte\[4];  
            byte\[] Nullable = { 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0 };  
            byte\[] Nullable2 = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };  
            codeN = rf\_rd\_keya\_card(18, 0, 1, 16, codeOrig);  
            for (int i = 0; i &lt; 4; i++)  
            {  
                testCode\[i] = codeOrig\[i];

            }  
            if (codeN == 0)  
            {

                if (codeOrig.Equals(Nullable) || codeOrig.Equals(Nullable2))     // 1块前几位如果是连续多个"0"或"F"，就表示没有数据，继续读下一块  
                {  
                    codeN = rf\_rd\_keya\_card(18, 0, 2, 16, codeOrig);  
                    isTeacher = true;  
                }              // 职工卡  
                else  
                {  
                    isTeacher = false;               // 学生卡  
                }

                CardBh = CardToBuaa(codeOrig, isTeacher, 1);  
                // 把从卡中读出的ID信息转换成校内的编号  
                CardValue = CardToBuaa(codeOrig, isTeacher, 2);

 

            }  
            else  
            {

            }  
            return CardValue;

        }  
        private string CardToBuaa(byte\[] chars, bool isTeacher, int flag)  
        {  
             
                //byte\[] target = {0x33,0x30,0x33,0x34,0x30,0x33,0x38,0x33,0x36,0x30,0,0,0,0,0,0};

                char\[] a = System.Text.Encoding.ASCII.GetChars(chars, 0, 18);  
                //int\[] b=new int\[16];  
                string med = new string(a);  
                //3034038360  
                string h = med.Substring(0, 2);  
                string l = med.Substring(2, 2);  
                string c = med.Substring(4, 2);  
                int len = int.Parse(c);  
                string e = "";  
                if (len &gt; 10)  
                {  
                      e = med.Substring(6, 12);  
                }  
                else {  
                      e = med.Substring(6, int.Parse(c));  
                }  
                byte a1 = byte.Parse(h, System.Globalization.NumberStyles.AllowHexSpecifier);  
                byte a2 = byte.Parse(l, System.Globalization.NumberStyles.AllowHexSpecifier);

                byte\[] head ={ a1, a2 };  
                char\[] headc = System.Text.Encoding.ASCII.GetChars(head, 0, head.Length);  
                string cardNum = new string(headc) + e;  
                string result = "";  
                  
                // bool isTeacher = true;  
                string masterType = "";  
                if (flag == 1)  
                {  
                    result = cardNum;

                }  
                else  
                {  
                    if (isTeacher)  
                    {  
                        result = cardNum;

                    }  
                    else  
                    {

                        string studentTypeStr = cardNum.Substring(0, 2);  
                        if (studentTypeStr.Equals("01"))  
                        {  
                            result = "BY" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

                        }  
                        else  
                        {  
                            if (studentTypeStr.Equals("11"))  
                            {

                                masterType = cardNum.Substring(8, 2);  
                                if (masterType.Equals("00"))  
                                {  
                                    result = "SY" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

 

                                }  
                                else  
                                {

                                    if (masterType.Equals("01"))  
                                    {  
                                        result = "MB" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

                                    }  
                                    else  
                                    {

                                        result = "不识别" + studentTypeStr + masterType;

                                    }

                                }

                            }  
                            else  
                            {

                                /////////////////////  
                                if (studentTypeStr.Equals("12"))  
                                {

                                    masterType = cardNum.Substring(8, 2);  
                                    if (masterType.Equals("00"))  
                                    {  
                                        result = "GS" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

 

                                    }  
                                    else  
                                    {

                                        if (masterType.Equals("01"))  
                                        {  
                                            result = "EM" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

                                        }  
                                        else  
                                        {

                                            result = "不识别" + studentTypeStr + masterType;

                                        }

                                    }  
                                }  
                                ////////////////  
                                else  
                                {

                                    /////////////////////  
                                    if (studentTypeStr.Equals("13"))  
                                    {

                                        masterType = cardNum.Substring(8, 2);  
                                        if (masterType.Equals("00"))  
                                        {  
                                            result = "DY" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

 

                                        }  
                                        else  
                                        {

                                            if (masterType.Equals("01"))  
                                            {  
                                                result = "JX" + cardNum.Substring(4, 4) + cardNum.Substring(11, 3);

                                            }  
                                            else  
                                            {

                                                result = "不识别" + studentTypeStr + masterType;

                                            }

                                        }  
                                    }  
                                    else  
                                    {

                                        if (studentTypeStr.Equals("22"))  
                                        {  
                                            result = "3" + cardNum.Substring(5, 3) + cardNum.Substring(10, 4);

                                        }  
                                        else  
                                        {

                                            if (studentTypeStr.Equals("24"))  
                                            {  
                                                result = "4" + cardNum.Substring(5, 3) + cardNum.Substring(10, 4);  
                                            }  
                                            else  
                                            {

                                                if (studentTypeStr.Equals("55"))  
                                                {  
                                                    result = "5" + cardNum.Substring(5, 3) + cardNum.Substring(10, 4);  
                                                }  
                                                else  
                                                {  
                                                    result = "不识别" + studentTypeStr;  
                                                }  
                                            }  
                                        }

                                    }

                                    ////////////////

 

 

                                }

 

                            }

 

 

                        }

                    }

                }  
                return result;  
           

 

        }

        
    }  
}