1

I have a program that gets a response from a url in binary format and I do not know how to convert this to a text file.

byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postString);
request.ContentLength = postBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(postBytes, 0, postBytes.Length);
stream.Close();

response = (HttpWebResponse)request.GetResponse();
Stream ReceiveStream = response.GetResponseStream();
string filename = "C:\\responseGot.txt";

byte[] buffer = new byte[1024];
FileStream outFile = new FileStream(filename, FileMode.Create);
int bytesRead;
while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0) 
    outFile.Write(buffer, 0, bytesRead);

When I open responseGot.txt it is a binary file how do I get text file.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2726975
  • 1,285
  • 3
  • 17
  • 26
  • You don't show how you write the file, but you'll have to know what format the binary is in in order to convert it to text. Try `Encoding.UTF8.GetString()` if it's UTF-8. – CodeCaster Dec 06 '13 at 11:05
  • @CodeCaster, this is how I write. byte[] buffer = new byte[1024]; FileStream outFile = new FileStream(filename, FileMode.Create); int bytesRead; while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0) outFile.Write(buffer, 0, bytesRead); – user2726975 Dec 06 '13 at 11:07
  • That answers half my comment. Do you know for sure that the bytes should represent text? Then call `GetString()` on the appropriate encoding class. – CodeCaster Dec 06 '13 at 11:12

4 Answers4

2

In what format is the response you get? There is no such thing as a text file. There are only binary files. HTTP is also 100% binary.

Text is the interpretation of bytes, and it only exists as part of running application. You can never, ever write text to a file. You can only convert the text to bytes (using various ways) and write the bytes.

Therefore, ask yourself why the bytes you received cannot be interpreted by notepad.exe as text. Maybe the response is not directly text but a ZIP file or something.

  1. You can guess the format with a hex editor
  2. You can ask the website owner
usr
  • 168,620
  • 35
  • 240
  • 369
  • 1
    YOU ARE SO AMAZING...saved my day thanks for the explanation.saved as zip file and voila it works – user2726975 Dec 06 '13 at 13:56
  • Set HttpWebRequest.AutomaticDecompression to true to do that automatically. The web server's response is probalby just gzipped (using `Content-Encoding`). This is a standard HTTP feature. – usr Dec 06 '13 at 14:01
0

You don't show in your code sample saving the file anywhere.

But to convert the response to string you can use:

   using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)   
    {  
      StreamReader reader = new StreamReader(response.GetResponseStream());
      string ResponseTXT = reader.ReadToEnd();
    }

Then you can save it with usual techniques http://msdn.microsoft.com/en-us/library/6ka1wd3w%28v=vs.110%29.aspx

Did you mean that?

RRM
  • 3,371
  • 7
  • 25
  • 40
  • The problems seems to be that the bytes received are do not correspond to readable text. The StreamReader will read garbage just as notepad.exe seems to do. – usr Dec 06 '13 at 11:26
0

Every data represented in digital computing these days is based on 2 bits ie. binary (electrical/magnetic signals: on/off or north/south). Every file written to disk is also a binary file ie. a sequence of (8 bit) bytes. ASCII/ANSI defines character map for each byte sequence and only about 95 of the 256 bytes are referred to as printable (text) characters.

Your downloaded file seems to have more than just the printable characters (usually referred to as a plain text file). To view the file as it is (in your current encoding settings):

type <file.ext>

To view in a different code page:

chcp <codepage>
type <file.ext>

To view a (plain)text representation of your file, you'd encode it first (ie. translate it to a text file) eg. hex coded string via some hex editor. The first few characters of the hex sequence should give a magic number, indicating the type of file being read. You'd then open the file with the associated program (that is capable of opening those types of files).

If it is a text file you were expecting and instead got a file which has more than just printable (plain text) characters, then it's more likely there has been some sort of compression/encryption applied to it. Once again, the magic number should hint how the file should be treated eg. decompressed before attempting to read the data/file. (Encrypted files should come with a decryption hint/key, unless exchanged/agreed earlier)

Zimba
  • 2,854
  • 18
  • 26
-1

Use the ReadFully method in this topic Creating a byte array from a stream

Get the string representation to an actual string:

string text = System.Text.Encoding.Default.GetString(byteArray);

And finally create the text file and write the content:

using(StreamWriter sw = new StreamWriter("C:\\responseGot.txt"))
{
        sw.WriteLine(text);
}
Community
  • 1
  • 1
Jota WA
  • 64
  • 3
  • You are converting bytes to a string then to the same bytes. It does not change anything. – usr Dec 06 '13 at 11:22
  • I'm converting a `Stream` to an array of `bytes`,then decoding the array to a `string` and finally writing the string to a text file.In this sequence when do I reconvert the string to an array of bytes? – Jota WA Dec 06 '13 at 11:28
  • The StreamWriter does that. – usr Dec 06 '13 at 12:18