将二进制文件转换为文本 [英] convert binary file to text

查看:1333
本文介绍了将二进制文件转换为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序可以从URL获得二进制格式的响应,但我不知道如何将其转换为文本文件.

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);

当我打开responseGot.txt时,它是一个二进制文件,我如何获取文本文件.

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

推荐答案

您收到的回复格式是什么? 没有文本文件.只有二进制文件. HTTP也是100%二进制的.

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.

因此,请问自己为什么notepad.exe无法将收到的字节解释为文本.也许响应不是直接文本,而是ZIP文件或其他内容.

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. 您可以使用十六进制编辑器猜测格式
  2. 您可以询问网站所有者

这篇关于将二进制文件转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆