c#读取bin文件

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace open_bin

{

public partial class BIN_LOAD : Form

{

public BIN_LOAD()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

//原版,打开几十kb的文件时间还行,上百kb之后时间就慢的卡住,主要是Mytext += j.ToString("X2");处理时间长,牵扯到垃圾回收机制

//string Mytext = "";

//int file_len;

//byte\[\] binchar = new byte\[\] { };

//FileStream Myfile = new FileStream("test2.bin", FileMode.Open, FileAccess.Read);

//BinaryReader binreader = new BinaryReader(Myfile);

//file_len = (int)Myfile.Length;//获取bin文件长度

//binchar = binreader.ReadBytes(file_len);

//foreach (byte j in binchar)

//{

// Mytext += j.ToString("X2");

// Mytext += " ";

//}

//textBox1.Text = Mytext;

//binreader.Close();

//改进版,读取上百kb的文件执行时间可以接受,StringBuilder执行效率远大于Mytext += j.ToString("X2")。

int file_len;

byte\[\] binchar = new byte\[\] { };

FileStream Myfile = new FileStream("test2.bin", FileMode.Open, FileAccess.Read);

BinaryReader binreader = new BinaryReader(Myfile);

file_len = (int)Myfile.Length;//获取bin文件长度

StringBuilder str = new StringBuilder();

binchar = binreader.ReadBytes(file_len);

foreach (byte j in binchar)

{

str.Append(j.ToString("X2"));

str.Append(" ");

}

textBox1.Text = str.ToString();

binreader.Close();

}

}

}

相关推荐
aramae7 小时前
模拟实现strcmp()(C语言)
c语言·开发语言·后端
泡海椒8 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
Beyond_System|系统之外9 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙552310 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
c#上位机10 小时前
C#上位机项目实战——Task.Run执行带参数的方法
c#·上位机
Bruce_Liuxiaowei12 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
aramae14 小时前
MySQL内置函数(7)
开发语言·笔记·后端·mysql·其他
Lost of 程序猿16 小时前
用 YARP 为 .NET 微服务搭一座“立交桥“:路由转发与生产配置实践
c#·asp.net
码行山野赴时序归途16 小时前
链表家族收官篇:循环链表
c语言·开发语言·数据结构·链表
景熙552317 小时前
单体项目编写思路和落地形式
java·开发语言