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. | | | | re: Convert Binary Data to ASCII
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
"as400tips@lycos.com" wrote: Quote:
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.
>
>
| | | | re: Convert Binary Data to ASCII
<as400tips@lycos.comwrote: Quote:
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 - <skeet@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 | | | | re: Convert Binary Data to ASCII
Jon wrote: Quote:
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","","","","","","","" | | | | re: Convert Binary Data to ASCII
<as400tips@lycos.comwrote: Quote: Quote:
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 - <skeet@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 | | | | re: Convert Binary Data to ASCII
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? | | | | re: Convert Binary Data to ASCII
<as400tips@lycos.comwrote: Quote:
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 - <skeet@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 |  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|