[英]String of bytes to byte array c#
In my web application, I have a connection to a web service. 在我的Web应用程序中,我已连接到Web服务。 In my Web Service, it has a method for getting the bytes of a report, based on the path: 在我的Web服务中,它具有一种基于路径获取报告字节的方法:
public byte[] GetDocument(string path)
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
return bytes;
}
Now, when I make a request from my web application, the web service gives me a Json object, similar to this: 现在,当我从Web应用程序发出请求时,Web服务会给我一个Json对象,类似于以下内容:
{
"Report":"0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAA
AAACAAAA3QAAAAAAAAAAEAAA3wAAAAEAAAD+ ... (continues)"
}
And in my web application I have a method for fetching the Json object, putting the "report" in a string. 在我的Web应用程序中,我有一种方法来获取Json对象,并将“报告”放入字符串中。 The web application then has a method for parsing the string of bytes into an array of bytes, which does not work, it throws a FormatException
: 然后,Web应用程序具有将字节字符串解析为字节数组的方法,该方法不起作用,它将引发FormatException
:
public byte[] DownloadReport(string id, string fileName)
{
var fileAsString = _api.DownloadReport(id, fileName);
byte[] report = fileAsString.Split()
.Select(t => byte.Parse(t, NumberStyles.AllowHexSpecifier))
.ToArray();
return report;
}
I have also tried to do this: 我也尝试这样做:
public byte[] DownloadReport(string id, string fileName)
{
var fileAsString = _api.DownloadReport(id, fileName);
byte[] report = Encoding.ASCII.GetBytes(fileAsString);
return report;
}
Which gave me a .doc file with the exact same string as the Json object had. 这给了我一个.doc文件,该文件具有与Json对象完全相同的字符串。
Am I parsing something the wrong way from the web service, or is it when I want to convert it to an array of bytes again? 我是从Web服务解析错误的方法,还是当我想再次将其转换为字节数组时?
public byte[] DownloadReport(string id, string fileName)
{
var fileAsString = _api.DownloadReport(id, fileName);
byte[] report = Convert.FromBase64String(fileAsString);
return report;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.