Convert Binary Data to ASCII

A

as400tips

I have a Binary Data file (Packed Decimal and ASCII mixed) and would
like to convert into ASCII (readable) file.

How to do it in C#? Thanks.
 
G

Guest

Read the file (or section of it) to a byte array, then pass it to
System.Text.Encoding.ASCII.GetString() to return a string containing the
text. Then you can write this to a file or display it or whatever.


HTH


Ciaran O'Donnell
 
J

Jon Skeet [C# MVP]

I have a Binary Data file (Packed Decimal and ASCII mixed) and would
like to convert into ASCII (readable) file.

How to do it in C#? Thanks.

It's not entirely clear what you mean. If there's some bits which are
genuinely ASCII, those should presumably be decoded as ASCII. If there
are some bits which are genuinely binary data, you need to work out how
you want to display that data.
 
A

as400tips

Jon said:
It's not entirely clear what you mean. If there's some bits which are
genuinely ASCII, those should presumably be decoded as ASCII. If there
are some bits which are genuinely binary data, you need to work out how
you want to display that data.

Here is the data stored in binary:

" "," ",""X","","","2",""
" "," ",""5","","","2",""
"CC"," ",""6","A","™™™™","ñ","","","","",""
" "," )"," ","","ò","&RA","","0","170","1","1","
170","","ð","","","","",""
 
J

Jon Skeet [C# MVP]

Here is the data stored in binary:

" "," ",""X","","","2",""
" "," ",""5","","","2",""
"CC"," ",""6","A","????","ñ","","","","",""
" "," )"," ","","ò","&RA","","0","170","1","1","
170","","ð","","","","",""

Well, that's not terribly helpful for two reasons:

1) The binary data has already been lost in converting your article
into text

2) Seeing the binary data accurately doesn't tell us how you want it to
be displayed
 
A

as400tips

Here is a code which suppose to convert:

FileStream iFile = new FileStream(@"c:\test\binary.dat",
FileMode.Open);

long lengthInBytes = iFile.Length;

BinaryReader bin = new BinaryReader(aFile);

byte[] byteArray = bin.ReadBytes((int)lengthInBytes);

System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;

string str = encEncoder.GetString(byteArray);


str converts first two binary data even though file has over 4000
bytes. What's going on?
 
J

Jon Skeet [C# MVP]

Here is a code which suppose to convert:

FileStream iFile = new FileStream(@"c:\test\binary.dat",
FileMode.Open);

long lengthInBytes = iFile.Length;

BinaryReader bin = new BinaryReader(aFile);

byte[] byteArray = bin.ReadBytes((int)lengthInBytes);

System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;

string str = encEncoder.GetString(byteArray);

str converts first two binary data even though file has over 4000
bytes. What's going on?

Treating arbitrary binary data as ASCII text is a very bad idea. Binary
data is *not* ASCII text. You need to work out how you want to
represent that binary data as text, and act accordingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top