二进制文件读取和拆分和转换 [英] Binary File Read and Split and Convert

查看:120
本文介绍了二进制文件读取和拆分和转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试阅读二进制文件并将其转换为ascii ....

我试图完成两个步骤: -



1.读取二进制文件并连续分割前4个字节,然后连续分割32个字节10次。

2.然后需要将各个位转换为ascii。



附上的图片将提供有关上述内容的更多信息。

(http://postimg.org/image/at015uhn1/)

http://postimg.org/image/at015uhn1/



问题我面临的是能够读取二进制文件,但我无法拆分和转换。



我使用的代码片段: -

Hi im trying to read a binary file and convert it to ascii....
there are two steps im trying to accomplish:-

1. Read the binary file and split first 4 bytes and then consecutive 32 bytes continuously for 10 times.
2. The individual bits then needs to be converted to ascii.

The image attached will give more information on the above.
(http://postimg.org/image/at015uhn1/)
http://postimg.org/image/at015uhn1/

Issue what im facing is im able to read the binary file but im not able to split and convert.

Snippet what im using :-

var fs = new FileStream(@"C:\csharpcorner.bin", FileMode.Open);

var len = (int)fs.Length;

var bits = new byte[len];

fs.Read(bits, 0, len);

// Dump 16 bytes per line

for (int ix = 0; ix < len; ix += 16)
{

var cnt = Math.Min(16, len - ix);

var line = new byte[cnt];

Array.Copy(bits, ix, line, 0, cnt);

// Write address + hex + ascii

Console.Write("{0:X6} ", ix);

Console.Write(BitConverter.ToString(line));

Console.Write(" ");

// Convert non-ascii characters to .

for (int jx = 0; jx < cnt; ++jx)

if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';

console.writeline(Encoding.ASCII.GetString(line));
}
Console.Readline();




推荐答案

您可能会发现这个方便: ByteArrayBuilder - 字节的StringBuilder [ ^ ] - 它允许你很容易地做到这一点。
You might find this handy: ByteArrayBuilder - a StringBuilder for Bytes[^] - it allows you do do that pretty easily.


这篇关于二进制文件读取和拆分和转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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