• 大小: 0.04M
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2024-04-29
  • 语言: C#
  • 标签: bin  HEX  程序  HEX  

资源简介

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());
            }

资源截图

代码片段和文件信息

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;


namespace Hex2Bin
{
    public partial class frMain : Form
    {
        String szBinPath = ““;
        String szHexPath = ““;
        public frMain()
        {
            InitializeComponent();
        }

        private void btnOpenHex_Click(object sender EventArgs e)
        {

            try
            {
                pbConvert.Value = 0;
                if (openHexDlg.ShowDialog() == DialogResult.OK) //打开转换的目标文件
                {
                    szHexPath = openHexDlg.FileName;
                    tbHexPath.Text = szHexPath;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnConvert_Click(object sender EventArgs e)
        {

            try
            {
                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 Lengt

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        120  2009-01-08 16:38  Hex2Bin\Hex2Bin\app.config

     文件      14328  2009-01-08 16:38  Hex2Bin\Hex2Bin\bin\Debug\Hex2Bin.vshost.exe

     文件        120  2009-01-08 16:38  Hex2Bin\Hex2Bin\bin\Debug\Hex2Bin.vshost.exe.config

     文件        490  2007-07-21 02:33  Hex2Bin\Hex2Bin\bin\Debug\Hex2Bin.vshost.exe.manifest

     文件      11264  2009-01-08 16:39  Hex2Bin\Hex2Bin\bin\Release\Hex2Bin.exe

     文件        120  2009-01-08 16:38  Hex2Bin\Hex2Bin\bin\Release\Hex2Bin.exe.config

     文件      24064  2009-01-08 16:39  Hex2Bin\Hex2Bin\bin\Release\Hex2Bin.pdb

     文件       4076  2009-01-08 16:36  Hex2Bin\Hex2Bin\frMain.cs

     文件       8405  2009-01-08 16:36  Hex2Bin\Hex2Bin\frMain.Designer.cs

     文件       6384  2009-01-08 16:36  Hex2Bin\Hex2Bin\frMain.resx

     文件       3705  2009-01-08 16:38  Hex2Bin\Hex2Bin\Hex2Bin.csproj

     文件         74  2009-01-06 15:26  Hex2Bin\Hex2Bin\Hex2Bin.csproj.user

     文件        309  2009-01-08 15:08  Hex2Bin\Hex2Bin\obj\Debug\Hex2Bin.csproj.FileListAbsolute.txt

     文件        848  2009-01-08 15:08  Hex2Bin\Hex2Bin\obj\Debug\Hex2Bin.csproj.GenerateResource.Cache

     文件        180  2009-01-08 15:08  Hex2Bin\Hex2Bin\obj\Debug\Hex2Bin.frMain.resources

     文件        180  2009-01-08 15:08  Hex2Bin\Hex2Bin\obj\Debug\Hex2Bin.Properties.Resources.resources

     文件       1691  2009-01-08 16:38  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.csproj.FileListAbsolute.txt

     文件        906  2009-01-08 16:36  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.csproj.GenerateResource.Cache

     文件      11264  2009-01-08 16:39  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.exe

     文件        180  2009-01-08 16:39  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.frMain.resources

     文件      24064  2009-01-08 16:39  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.pdb

     文件        180  2009-01-08 16:39  Hex2Bin\Hex2Bin\obj\Release\Hex2Bin.Properties.Resources.resources

     文件        469  2009-01-08 09:55  Hex2Bin\Hex2Bin\Program.cs

     文件       1392  2009-01-06 14:24  Hex2Bin\Hex2Bin\Properties\AssemblyInfo.cs

     文件       2865  2009-01-06 14:24  Hex2Bin\Hex2Bin\Properties\Resources.Designer.cs

     文件       5612  2009-01-06 14:23  Hex2Bin\Hex2Bin\Properties\Resources.resx

     文件       1092  2009-01-06 14:24  Hex2Bin\Hex2Bin\Properties\Settings.Designer.cs

     文件        249  2009-01-06 14:23  Hex2Bin\Hex2Bin\Properties\Settings.settings

     文件        915  2009-01-06 15:26  Hex2Bin\Hex2Bin.sln

    ..A..H.     14336  2009-01-08 16:43  Hex2Bin\Hex2Bin.suo

............此处省略16个文件信息

评论

共有 条评论