jack jack

jack jack

  • NA
  • 2
  • 4.1k

Binary File Read and Split and Convert

Feb 11 2014 12:21 PM
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/)

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

Answers (1)