Tutorial :How can I covert a binary file into a set of ascii charecters



Question:

I want to convert a binary file into an array of ascii charcters . how can I do that . thank you .


Solution:1

It depends on what you want to do with it. Ascii is supposed to be 7bits (0-127 are well defined, the other characters are codepage dependant). So plain ASCII encoding can lead to nasty surprises (among which are non printables characters as nulls...)

If you want to have something printable out of your byte array, you should not convert them with an ASCII encoding. You'd better encode it in Base64, which is a safe (albeit not too optimal size-wise) way to encode binary in strings.

To encode your bytes in Base64, you can just go with:

string result = System.Convert.ToBase64String(yourByteArray);  


Solution:2

Check out BASE64 or UUEncoding. I assume you're wanting to use only printable characters from the 256-char ASCII set.

BASE64 uses only 64 characters (sometimes this is used when sending binary via email for example). This causes the output to grow in size -- something you have to consider in your situation.


Solution:3

StreamReader reader = new StreamReader("pathtoyourbinaryfile", System.Text.Encoding.ASCII);  char[] text = reader.ReadToEnd().ToCharArray();  


Solution:4

You can read each character and just cast it to a char. That would convert every byte to an ascii character. Is that what you are looking to do?


Solution:5

I do not believe that earlier responders answered the question. Many people say ASCII when they mean byte so I suspect the questioner wanted to read a binary file and not a text file with encoded characters.

To demonstrate the impact of different techniques I created a UTF-8 text file from the following string:

  string s = "\ta£\x0394\x221A";   // tab; lower case a; pound sign; Greek delta; square root    

The following program reads this file first as a text string then as an array of bytes. In each case it displays the input first as decimal and then as hexadecimal.

  String test = "";    String fileStg = File.ReadAllText("Test.txt");    for (int i = 0; i < fileStg.Length; i++)      test += (int)fileStg[i] + " ";    Debug.Print(test);    test = "";    for (int i = 0; i < fileStg.Length; i++)      test += ((int)fileStg[i]).ToString("X") + " ";    Debug.Print(test);    test = "";    Byte[] fileByte = File.ReadAllBytes(pathProg + "Test.txt");    for (int i = 0; i < fileByte.Length; i++)      test += fileByte[i].ToString() + " ";    Debug.Print(test);    test = "";    for (int i = 0; i < fileByte.Length; i++)      test += fileByte[i].ToString("X") + " ";    Debug.Print(test);  

Output:

9 97 163 916 8730 13 10  9 61 A3 394 221A D A  239 187 191 9 97 194 163 206 148 226 136 154 13 10  EF BB BF 9 61 C2 A3 CE 94 E2 88 9A D A  

Note that when read as a text file, the BOM has been stripped off and multibyte UTF-8 characters have become single characters in the input string. But when read as an array of bytes, I receive the BOM and the raw UTF-8.

I suspect the questioner wanted to read a genuine binary file rather than a Unicode text file but I believe this example better illustrates the effects of the two techniques and demonstrates that if the questioner wishes to read a binary file then ReadAllBytes is the more appropriate technique.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »