Click here to Skip to main content
15,895,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :-
C#
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();

[Edit: M.T.Heffron formatted code]
Posted
Updated 11-Feb-14 6:35am
v2
Comments
Matt T Heffron 11-Feb-14 12:38pm    
Describe specifically what isn't working.
"not able to split and convert" isn't clear
We shouldn't need to run your code to see the problem (and we don't have the input file anyway)!

1 solution

You might find this handy: ByteArrayBuilder - a StringBuilder for Bytes[^] - it allows you do do that pretty easily.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900