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

}

}

}

相关推荐
MAGICIAN...2 分钟前
【java-软件设计原则】
java·开发语言
gpfyyds66610 分钟前
Python代码练习
开发语言·python
盐真卿26 分钟前
python第八部分:高级特性(二)
java·开发语言
茉莉玫瑰花茶28 分钟前
C++ 17 详细特性解析(5)
开发语言·c++·算法
lly20240635 分钟前
《堆的 shift down》
开发语言
czhc11400756631 小时前
通信 28
c#
黎雁·泠崖1 小时前
【魔法森林冒险】2/14 抽象层设计:Figure/Person类(所有角色的基石)
java·开发语言
季明洵1 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
墨雪不会编程1 小时前
C++之【深入理解Vector】三部曲最终章
开发语言·c++
浅念-2 小时前
C语言编译与链接全流程:从源码到可执行程序的幕后之旅
c语言·开发语言·数据结构·经验分享·笔记·学习·算法