文件转换为二进制数组文件工具
0.前言
最近需要将文件制转换为对应的数组文件,在网上找了下只有CSDN有的下.....额😂,索性我就自己写了一个,反正够用就行。
1.使用
- 打开转换工具后选择要转换的文件。
- 转换工具将自动转换,数组文件将保存在文件同级目录下
选择的文件名
+.h
文件中。
2.Source
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace bin_to_array
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
static int len;
static Byte[] _data; //转换后的数组数据
static Byte[] data; //读入的二进制文件
static string path; //保存路径
static int x;
static FileStream file;
static Thread t;
private void Form1_Shown(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
file = new FileStream(this.openFileDialog1.FileName, FileMode.Open);
BinaryReader br = new BinaryReader(file);
data = br.ReadBytes((int)file.Length);
_data = new byte[file.Length * 5+14+ file.Length/16+((file.Length%16)==0?0:1)];
len = ((int)file.Length);
t = new Thread(bin_to_array);
t.Start();
timer1.Enabled = true;
}
catch (Exception exc)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(exc.Message);
}
}
else
{
this.Close();
}
}
public static void bin_to_array()
{
int H4;
int L4;
int p = 12;
_data[0] = (byte)'c';
_data[1] = (byte)'h';
_data[2] = (byte)'a';
_data[3] = (byte)'r';
_data[4] = (byte)' ';
_data[5] = (byte)'d';
_data[6] = (byte)'a';
_data[7] = (byte)'t';
_data[8] = (byte)'a';
_data[9] = (byte)'[';
_data[10] = (byte)']';
_data[11] = (byte)'=';
_data[_data.Length - 2] = (byte)'}';
_data[_data.Length - 1] = (byte)';';
for (x = 0; x < len; x++)
{
if (x % 16 == 0)
{
_data[p] = (byte)'\n';
p += 1;
}
//"char data[]="长度12
_data[0+p] = 0x2C; //,
_data[1+p] = 0x30; //0
_data[2+p] = 0x78; //x
H4 = data[x] / 16;
L4 = data[x] % 16;
if (H4 < 10)
_data[3+p] = (byte)(0x30 + H4); //0
else
_data[3+p] = (byte)(0x41 + H4 - 10);
if (L4 < 10)
_data[4+p] = (byte)(0x30 + L4);
else
_data[4+p] = (byte)(0x41 + L4 - 10);
p += 5;
}
_data[13] = (byte)'{';
}
static void save(String path,byte[] data_)
{
try
{
BinaryWriter bw = new BinaryWriter(new FileStream(path,FileMode.Create));
bw.Write(_data);
bw.Close();
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
textBox1.Text = x.ToString() + "/" + len.ToString();
Console.WriteLine(x.ToString() + "/" + len.ToString());
if (t.ThreadState == ThreadState.Stopped)
{
textBox1.Text = x.ToString() + "/" + len.ToString();
textBox1.Text += "\r\nSave file...";
path = this.openFileDialog1.FileName;
path = path + ".h";
save(path, _data);
file.Close();
textBox1.Text += "\r\nComplete!!!";
return;
}
timer1.Enabled = true;
}
}
}
链接:https://pan.baidu.com/s/1MU5BJV00Chu12u7ksDOqWQ
提取码:5k6g
3.成品
链接:https://pan.baidu.com/s/1J3mEOMCxNeiAhVgnMgAXRg
提取码:nhjy
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。