Connecting Tech Pros Worldwide Forums | Help | Site Map

Convert Binary Data to ASCII

as400tips@lycos.com
Guest
 
Posts: n/a
#1: Sep 12 '06
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.


Ciaran O''Donnell
Guest
 
Posts: n/a
#2: Sep 12 '06

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.
>
>
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#3: Sep 12 '06

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
as400tips@lycos.com
Guest
 
Posts: n/a
#4: Sep 12 '06

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","","","","","","",""

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Sep 12 '06

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
as400tips@lycos.com
Guest
 
Posts: n/a
#6: Sep 12 '06

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?

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#7: Sep 12 '06

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
Closed Thread