简体   繁体   中英

Byte array to xml with UTF8 and base64 encoding?

    [return: System.Xml.Serialization.XmlElementAttribute("return", DataType="base64Binary")]
    public byte[] get(...)

I am trying to get a xml(utf-8) from this webservice. I have tried multiple things to try and get the xml out of the byte array like:

stream
encoding
decoder
converter

[Extra info] When decoding the byte array with Encoding.UTF8.GetString(bytes) I get a string with strange signs and symbols but also with some text Starting with: %PDF-1.4

[SOLUTION] Writing the byte array to a pdf file makes it readable.

您可以尝试Convert.ToBase64String

I think the web service provides a byte stream that is simply base64-encoded data represented as integers instead of chars. I believe that the base64 chars are a subset of ASCII, so you need to convert the byte array to ASCII (ie base64 represented as chars), then convert these chars from base64:

var base64AsAscii = Encoding.ASCII.GetString(bytesFromWebService);
var decodedBytes = Convert.FromBase64String(bytesAsAscii);
var text = Encoding.UTF8.GetString(decodedBytes);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM