txt2bin

           StreamReader sr = new StreamReader(@"C:UserswoldyDesktopsqlite.txt");
            FileStream sw = File.Open(@"C:UserswoldyDesktopsqlite.bin2", FileMode.OpenOrCreate,FileAccess.Write);
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                string newline = line.Substring(7); //截掉HEX编辑器位移信息字符
                byte[] returnBytes = new byte[newline.Length / 2]; 
                for (int i = 0; i < newline.Length / 2; i++)
                {
                    //returnBytes[i] = Convert.ToByte("0x" + newline.Substring(i * 2, 2), 16);
                    if (i % 2 == 0) //有时候高位低位会互换
                    {
                        returnBytes[i] = Convert.ToByte("0x" + newline.Substring((i+1) * 2, 2), 16);
                    }
                    else
                    {
                        returnBytes[i] = Convert.ToByte("0x" + newline.Substring((i - 1) * 2, 2), 16);
                    }
                    
                }
                sw.Write(returnBytes, 0, returnBytes.Length);
                
            }
            sw.Close();
            sr.Close();
        }

发表评论