468,738 Members | 1,653 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Convert Binary Data to ASCII

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.

Sep 12 '06 #1
6 27015
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

"as*******@lycos.com" wrote:
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.

Sep 12 '06 #2
<as*******@lycos.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #3

Jon wrote:
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","","","","","","",""

Sep 12 '06 #4
<as*******@lycos.comwrote:
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","","","","","","",""
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #5
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?

Sep 12 '06 #6
<as*******@lycos.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by J. Campbell | last post: by
13 posts views Thread by Hako | last post: by
13 posts views Thread by HNT20 | last post: by
29 posts views Thread by Harlin Seritt | last post: by
19 posts views Thread by Serman D. | last post: by
5 posts views Thread by =?Utf-8?B?YmJkb2J1ZGR5?= | last post: by
4 posts views Thread by Mason | last post: by
1 post views Thread by CARIGAR | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.