将.hex文件从磁盘转换为.bin文件 [英] Converting .hex file from the disk into .bin file

查看:86
本文介绍了将.hex文件从磁盘转换为.bin文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我需要将.hex文件(整个文件不只是几个字节)转换为二进制文件.输入应该从磁盘上的用户那里获得.然后,应将同一文件转换为二进制文件(.bin文件).我尝试了一个可以从磁盘获取文件的代码.之后,我无法弄清楚应该如何读取,转换和然后以.bin格式存储文件.有人可以帮我吗?

提前谢谢.

Hi all

I need to convert a .hex file( a whole file not just few bytes) into a binary file. The input should be got from the user from the disk. Then the same file should be converted into a binary file(.bin file). I have tried a code where I am able to get the file from the disk. After which I''m not able to figure out how the file should be read, converted and then stored again as a .bin format. Can anyone help me in this?

Thanks in advance.

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private byte[] mBuffer;
        private void btnOpen_Click(object sender, EventArgs e)
        {
            Stream myStream;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:";
            openFileDialog1.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                    using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open))
                    {
                        mBuffer = new byte[fs.Length];
                        fs.Read(mBuffer, 0, (int)fs.Length);
                        fs.Close();
                    }
            }
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            if (saveFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create))
            {
                String binaryval="",hexvalue="";        
                binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);

            }     
       }
}

推荐答案

一些建议-由于这是您的家庭作业,因此您应该自己做一些!
1)读取十六进制文件:查看 File.ReadAllText [ ^ ],或者 File.ReadAllLines [ Convert.ToByte [^ ](字符串,每个十六进制字符对的16)-同样,在不知道您的十六进制格式的情况下,很难具体说明.
3)使用 File.WriteAllBytes [ ^ ]
A couple of suggestions - since this is your homework, you should do some of it yourself!

1) Read the hex file: Look at File.ReadAllText[^], or File.ReadAllLines[^] depending on how your hex files are organised.
2) Loop thorugh the file, converting the hex to bytes, using Convert.ToByte[^](string, 16) for each hex character pair - again, without knowing your hex format, it is difficult to be specific.
3) Write the bytes using File.WriteAllBytes[^]


此代码对我有用...它从磁盘上获取文件(.hex)并转换为.bin格式...在此处发布,以便它对某人提供帮助:)

使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;




命名空间WFAConvertor
{
公共局部类Form1:Form
{
公共Form1()
{
InitializeComponent();
}
专用字节[] mBuffer;
private void button1_Click(对象发送者,EventArgs e)
{
流myStream;
OpenFileDialog openFileDialog1 =新的OpenFileDialog();

openFileDialog1.InitialDirectory ="C:\\";
openFileDialog1.Filter =十六进制文件(* .hex)| * .hex |所有文件(*.*)| *.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

如果(openFileDialog1.ShowDialog()== DialogResult.OK)
{
试试
{
如果((myStream = openFileDialog1.OpenFile())!= null)
使用(FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read))
{
mBuffer =新字节[fs.Length];
fs.Read(mBuffer,0,(int)fs.Length);
fs.Close();
}



//FileStream fsWrite = new FileStream(openFileDialog1.InitialDirectory +"\\" +"temp.bin",FileMode.Create,FileAccess.Write);
StringBuilder sbWrite = new StringBuilder();
使用(StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
字符串binaryval =",hexvalue =";
//binaryval = Convert.ToString(Convert.ToInt32(hexvalue,16),2);

字符串行;
//从文件读取并显示行,直到
结束 //已到达文件.
while((line = sr.ReadLine())!= null)
{
binaryval =";
line = line.Substring(1);
char [] charArray = line.ToCharArray();
for(int i = 0; i< charArray.Length; i ++)
{
binaryval + = Convert.ToString(Convert.ToInt32(charArray [i] .ToString(),16),2);
}

sbWrite.AppendLine(binaryval);



}
使用(StreamWriter outfile =
新的StreamWriter(openFileDialog1.InitialDirectory +"\\" +"temp.bin"))
{
outfile.Write(sbWrite.ToString());
}

}


}
捕获(IOException ex)
{
}
}
}
}}}
This code works for me... it gets a file(.hex) from the disk and converts into .bin format... Postin it here so it mite help someone :)

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




namespace WFAConvertor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private byte[] mBuffer;
private void button1_Click(object sender, EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "C:\\";
openFileDialog1.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read))
{
mBuffer = new byte[fs.Length];
fs.Read(mBuffer, 0, (int)fs.Length);
fs.Close();
}



//FileStream fsWrite = new FileStream(openFileDialog1.InitialDirectory + "\\" + "temp.bin", FileMode.Create, FileAccess.Write);
StringBuilder sbWrite = new StringBuilder();
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
String binaryval = "", hexvalue = "";
// binaryval = Convert.ToString(Convert.ToInt32(hexvalue, 16), 2);

String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
binaryval = "";
line = line.Substring(1);
char[] charArray = line.ToCharArray();
for (int i = 0; i < charArray.Length; i++)
{
binaryval += Convert.ToString(Convert.ToInt32(charArray[i].ToString(), 16), 2);
}

sbWrite.AppendLine(binaryval);



}
using (StreamWriter outfile =
new StreamWriter(openFileDialog1.InitialDirectory + "\\" + "temp.bin"))
{
outfile.Write(sbWrite.ToString());
}

}


}
catch (IOException ex)
{
}
}
}
} }}


这篇关于将.hex文件从磁盘转换为.bin文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆