资 源 简 介
hex转bin的程序, 支持大于64K的hex, 网上下载的Hex2Bin不支持64k. 包含工程文件, 简单包装即可生成一个Hex2Bin的小程序【核心代码】 String szLine = ""; String szHex = ""; if (szHexPath == "") { MessageBox.Show("请选择需要转换的目标文件! ", "错误"); return; } StreamReader HexReader = new StreamReader(szHexPath); while (true) { szLine = HexReader.ReadLine(); //读取一行数据 if (szLine == null) //读完所有行 { break; } if (szLine.Substring(0, 1) == ":") //判断第1字符是否是: { if (szLine.Substring(1, 8) == "00000001")//数据结束 { break; } szHex = szLine.Substring(9, szLine.Length - 11); //读取有效字符 } } HexReader.Close(); //关闭目标文件 Int32 i; Int32 j = 0; Int32 Length = szHex.Length; byte[] szBin = new byte[Length]; pbConvert.Maximum = Length / 2; for (i = 0; i < Length; i = 2) //两字符合并成一个16进制字节 { szBin[j] = (byte)Int16.Parse(szHex.Substring(i, 2), NumberStyles.HexNumber); j ; pbConvert.Increment(i); } if (szBinPath == "") { szBinPath = Path.ChangeExtension(szHexPath, "bin"); tbBinPath.Text = szBinPath; } FileStream fBin = new FileStream(szBinPath, FileMode.Create); //创建文件BIN文件 BinaryWriter BinWrite = new BinaryWriter(fBin); //二进制方式打开文件 BinWrite.Write(szBin, 0, Length); //写入数据 BinWrite.Flush();//释放缓存 BinWrite.Close();//关闭文件 MessageBox.Show("文件转换完成! ", "提示"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); }