472,993 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

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 27873
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prachi | last post by:
I want to convert the binary data to string. I tried doing the same using following peace of code Function SimpleBinaryToString(Binary) Dim I, S For I = 1 To LenB(Binary) S = S &...
10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
1
by: Adam | last post by:
Hello, I'm trying to decifer the data in the table that stores the data in the binary format. All numbers are placed in varbinary fields. All I know is the MS SQL 2000 database useing collation...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
13
by: HNT20 | last post by:
Hello All i am new to python language. i am working on a gnuradio project where it uses python as the primary programming language. i am trying to convert a message, text, or numbers into binary...
29
by: Harlin Seritt | last post by:
Hi... I would like to take a string like 'supercalifragilisticexpialidocius' and write it to a file in binary forms -- this way a user cannot read the string in case they were try to open in...
19
by: Serman D. | last post by:
Hi, I have very limited C knowledge. I want to convert to output from a MD5 hash algorithm to printable ascii similar to the output of the md5sum in GNU coreutils. Any help on how to do the...
5
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I am having a problem converting string to binary and I am hoping someone can help me out I have a sql query that does an update that updates a binary field calles password ...
4
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f =...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.