zoukankan      html  css  js  c++  java
  • fsn文件解析(C#)

      public class FsnBizNet
        {
            private static int count;
            public static int parseInt(IList<string> list)
            {
                int value = Convert.ToInt32(list[count + 1] + list[count], 2);
                count += 2;
                return value;
            }
            public static string parseString(IList<string> list)
            {
                string value = list[count + 3] + list[count + 2] + list[count + 1] + list[count];
                count += 4;
                return value;
            }
            public static List<string> readFileToBit(string filepath)
            {
                FileStream @in = new FileStream(filepath, FileMode.Open);
                List<string> list = new List<string>();
                long left = @in.Length;//文件大小
                byte[] buffer = new byte[1024];
                int start = 0;//读取位置
                int length = 0; //实际返回结果长度
                while (left > 0)
                {
                    @in.Position = start;
                    length = 0;
                    if (left < buffer.Length)
                        length = @in.Read(buffer, 0, Convert.ToInt32(left));
                    else
                        length = @in.Read(buffer, 0, buffer.Length);
                    if (length == 0)
                    {
                        break;
                    }
                    start = start + length;
                    left = left - length;
                    for (int i = 0; i < length; i++)
                    {
                        string s = Convert.ToString(buffer[i] | 256, 2);
                        s = s.Substring(s.Length - 8);
                        list.Add(s);
                    }
                }
                @in.Close();
                return list;
            }
            public static SnoHead getHead(IList<string> list)
            {
                count = 0;
                FsnBizNet f = new FsnBizNet();
                SnoHead sh = new FsnBizNet.SnoHead(f);
                int[] headStart = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    headStart[i] = parseInt(list);
                }
                sh.HeadStart = headStart;
                int[] headString = new int[6];
                for (int i = 0; i < 6; i++)
                {
                    headString[i] = parseInt(list);
                }
                sh.HeadString = headString;
                long counter = parseInt(list) + (parseInt(list) << 8);
                sh.Counter = counter;
                int[] headEnd = new int[4];
                for (int i = 0; i < 4; i++)
                {
                    headEnd[i] = parseInt(list);
                }
                sh.HeadEnd = headEnd;
                return sh;
            }
            public static IDictionary getSnoExpImg(IList<string> list)
            {
                count = 0;
                IDictionary map = new Hashtable();
                // 设置日期时间
                int data = parseInt(list);
                int time = parseInt(list);
                int y = data >> 9;
                int m = (data - (y << 9)) >> 5;
                int d = data - (y << 9) - (m << 5);
                int hh = time >> 11;
                int mm = (time - (hh << 11)) >> 5;
                int ss = (time - (hh << 11) - (mm << 5)) << 1;
                map["DateTime"] = y + 1980 + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
                // 设置货币真、假、残和旧币标志
                map["TfFlag"] = parseInt(list);
                // 设置货币错误码(3个)
                string errorCode = "";
                for (int i = 0; i < 3; i++)
                {
                    int code = parseInt(list);
                    if (code < 13 && code > 0)
                    {
                        errorCode += code + ",";
                    }
                }
                if ("1".Equals(map["TfFlag"]))
                {
                    errorCode = errorCode.Substring(0, errorCode.LastIndexOf(","));
                }
                else
                {
                    errorCode = "0";
                }
                map["ErrorCode"] = errorCode;
                // 设置币种标志(4个)
                string moneyFlag = "";
                for (int i = 0; i < 4; i++)
                {
                    int flag = parseInt(list);
                    if (flag != 0)
                    {
                        moneyFlag += (char)flag;
                    }
                }
                map["MoneyFlag"] = moneyFlag;
                // 设置年版或版本号标志
                int ver = parseInt(list);
                if (ver == 0)
                {
                    ver = 1990;
                }
                if (ver == 1)
                {
                    ver = 1999;
                }
                if (ver == 2)
                {
                    ver = 2005;
                }
                if (ver == 9999)
                {
                    ver = 0;
                }
                map["Ver"] = ver;
                // 设置币值
                map["Valuta"] = parseInt(list);
                // 设置冠字号位数
                map["CharNum"] = parseInt(list);
                // 设置冠字号(12个)
                string no = "";
                for (int i = 0; i < 12; i++)
                {
                    int No = parseInt(list);
                    if (No != 0)
                    {
                        no += (char)No;
                    }
                }
                map["Sno"] = no;
                // 设置机具编号(24个)
                string machineSNo = "";
                for (int i = 0; i < 24; i++)
                {
                    int Msno = parseInt(list);
                    if (Msno != 0)
                    {
                        machineSNo += (char)Msno;
                    }
                }
                map["MachineSno"] = machineSNo;
                // 设置冠字号保留字
                map["Reserve1"] = parseInt(list);
                return map;
            }
            public static Bitmap getSnoImg(IList<string> list)
            {
                count = 0;
                int num = parseInt(list);
                int height = parseInt(list);
                int width = parseInt(list);
                int Reserve2 = parseInt(list);
                // 根据读取的信息创建图像缓冲区
                Brush bruch = Brushes.White;
                Bitmap image = new Bitmap(width * num, height, PixelFormat.Format32bppRgb);
                System.Drawing.Graphics g = Graphics.FromImage(image);
                g.FillRectangle(bruch, 0, 0, width * num, height);
                g.Dispose();
                int i = 0;
                while (list.Count - count > 0 && i < width * num)
                {
                    string s = parseString(list);
                    for (int j = 0; j < height && j < s.Length; j++)
                    {
                        if (s[j] == '1')
                        {
                             int colorvalue =Convert.ToInt32(0x000000);
                             image.SetPixel(i, j, Color.FromArgb(colorvalue));
                        }
                    }
                    i++;
                }
                return image;
            }
            public static List<IDictionary> readFile(string path)
            {
                // bit-list
                List<string> listBit = readFileToBit(path);
                // 头文件
                SnoHead sh = getHead(listBit.GetRange(0, 32-0));
                // 冠字号信息条数
                long counter = sh.Counter;
                // 根据冠字号头文件判断是否存在图像,得出一条冠字号信息包含的byte数
                int size = sh.HeadString[2] != 0x2D ? 1644 : 100;
                if (counter * size + 32 == listBit.Count)
                {
                    //string imagePath = Thread.CurrentThread.ContextClassLoader.getResource("").Path.Substring(1).Replace("%20", " ");
                    string imagePath = AppDomain.CurrentDomain.BaseDirectory.Substring(0).Replace("%20", " ");
                    imagePath = Path.Combine(imagePath,"image");
                    List<IDictionary> list = new List<IDictionary>();
                    for (int i = 0; i < counter; i++)
                    {
                        //listBit.ToArray().
                        IDictionary map = getSnoExpImg(listBit.GetRange(i * size + 32, i * size + 132 - (i * size + 32)));
                        if (size != 100)
                        {
                            Bitmap imageSno = getSnoImg(listBit.GetRange(i * size + 132, (i + 1) * size - (i * size + 132)));
                            imageSno.Save(Path.Combine(imagePath,i + ".bmp"), ImageFormat.Bmp);
                            map["ImageSno"] = "image/" + i + ".bmp";
                        }
                        else
                        {
                            map["ImageSno"] = null;
                        }
                        list.Add(map);
                    }
                    return list;
                }
                return null;
            }
            public class SnoHead
            {
                private readonly FsnBizNet outerInstance;
                public SnoHead(FsnBizNet outerInstance)
                {
                    this.outerInstance = outerInstance;
                }
                internal int[] headStart;
                internal int[] headString;
                internal long counter;
                internal int[] HeadEnd_Renamed;
                public virtual int[] HeadStart
                {
                    get
                    {
                        return headStart;
                    }
                    set
                    {
                        this.headStart = value;
                    }
                }
                public virtual int[] HeadString
                {
                    get
                    {
                        return headString;
                    }
                    set
                    {
                        this.headString = value;
                    }
                }
                public virtual long Counter
                {
                    get
                    {
                        return counter;
                    }
                    set
                    {
                        this.counter = value;
                    }
                }
                public virtual int[] HeadEnd
                {
                    get
                    {
                        return HeadEnd_Renamed;
                    }
                    set
                    {
                        HeadEnd_Renamed = value;
                    }
                }
            }
        }
  • 相关阅读:
    (转)CKEditor+CKFinder 配置
    asp.net一个带抽象工厂的三层架构完整实例
    windows phone 8 手机存储卡数据
    c#常用算法
    mvvm 事件命令
    windows phone 8 语音识别1
    windows phone 8 分辨率
    PHP通用防注入安全代码《转》
    SQL查询语句使用详解《转》
    PHP中文件包含语句的区别《转》
  • 原文地址:https://www.cnblogs.com/Charles2008/p/FSN.html
Copyright © 2011-2022 走看看