将字符串变量中接收的pdf文件转换为C#中的字节数组 [英] Convert pdf file received in string variable to byte array in C#
问题描述
我正在尝试用C#开发一个应用程序,该应用程序从Service1(第三方)获取数据,对其进行处理,然后将数据发送到Service2(再次由第三方).
I am trying to develop an application in C# which takes data from Service1(3rd party), processes it and then sends data to Service2(again 3rd party).
我尝试接收,处理和发送的数据是 pdf 文件
The data I am trying to receive, process and send is a pdf file
我从Service1收到了 string 变量中的pdf文件.
From Service1, I am receiving pdf file in a string variable.
例如
response.Content = "%PDF-1.4 \n1 0 obj\n<<\n/Pages 2 0 R\n/Type /Catalog\n>>\nendobj\n2 0 obj\n<<\n/Type /Pages\n/Kids [ 3 0 R 17 0 R ]\n/Count 2\n>>\nendobj\n3 0 obj\n<<\n/Type /Page\n/Parent 2 0 R\n/Resources <<\n/XObject << /Im0 8 0 R >>\n/ProcSet 6 0 R
>>\n/MediaBox [0 0 ..."
现在,Service2要求PDF数据采用 byte [] 格式
Now, Service2 requires PDF data to be in byte[] form
如何将 response.Content 即 string 转换为 byte []?
仅供参考-
我尝试了下面的方法,但是没有用.我可以打开文件,但显示所有垃圾值.
I have tried below method, but it didn't work. I am able to open file but it shows all junk values.
byte[] bitmapData = Encoding.UTF8.GetBytes(response.Content);
更新: 已解决
我只是发现不是Service1以字符串格式发送数据,只是我正在使用IRestResponse.Content
,这是 RestSharp 的字符串变量,而不是使用IRestResponse.RawBytes
,一个byte []变量.
I just found that it was not Service1 which was sending data in string format, it's just I was using IRestResponse.Content
, a string variable from RestSharp instead of using IRestResponse.RawBytes
, a byte[] variable.
推荐答案
我只是发现不是 Service1 以字符串格式发送数据,只是我正在使用IRestResponse.Content
, RestSharp 中的字符串变量,而不使用IRestResponse.RawBytes
字节[]变量.
I just found that it was not Service1 which was sending data in string format, it's just I was using IRestResponse.Content
, a string variable from RestSharp instead of using IRestResponse.RawBytes
, a byte[] variable.
这篇关于将字符串变量中接收的pdf文件转换为C#中的字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!