How to decode a string of text from a Base64 to a byte array, and the get the string property of this byte array without data corruption

后端 未结 3 1355
逝去的感伤
逝去的感伤 2020-12-16 15:48

Ok so I have a string of text, encoded in Base 64 like below:

string myText = \"abcBASE64TEXTGOESHEREdef==\";  // actual string is 381 characters long with t         


        
相关标签:
3条回答
  • 2020-12-16 16:41
    static void Main(string[] args)
        {
            string completeUrl = SERVICE_URL;
            WebRequest request = WebRequest.Create(completeUrl);
            request.Credentials = CredentialCache.DefaultCredentials;
            WebProxy proxyObject = new WebProxy(SERVICE_URL, true);
            request.Proxy = proxyObject;
            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            byte[] data;
            const int BUFFER_SIZE = 2048;
            int bytesRead;
            byte[] buffer = new byte[BUFFER_SIZE];
            using (MemoryStream mss = new MemoryStream())
            {
                using (BinaryReader readers = new
                BinaryReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8))
                {
                    while ((bytesRead = readers.Read(buffer, 0, BUFFER_SIZE)) > 0)
                    {
                        mss.Write(buffer, 0, bytesRead);
                    }
                    data = mss.ToArray();
                    System.Text.Encoding enc = System.Text.Encoding.GetEncoding("iso-8859-1");
                    string str = enc.GetString(data);
                    XmlDocument xdoc = new XmlDocument();
                    xdoc.LoadXml(str);
                    XmlNode xmlList = xdoc.ChildNodes[1];
                    XmlNode item = xmlList.ChildNodes[1]; //this is your data : JVBERi0xLjUNCiXNzc3NDQoxIDAgb2JqDQo8PA0KL0NyZWF0b3IgKERvY3VtZW50UHJ
                    Base64Decode(item.InnerText.ToString());
                    Console.WirteLine("File successfully saved");
                    Console.ReadLine();
                }
            }
        }
    
        public static void Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            File.WriteAllBytes("pdf.pdf", base64EncodedBytes);
        }
    
    0 讨论(0)
  • 2020-12-16 16:42

    If the string is encoded then the contents would look much like what you have in your text file. But to ensure that the file is not getting corrupt you should write the file content as binary instead of using a text encoder. Check out File.WriteAllBytes().

    0 讨论(0)
  • 2020-12-16 16:42
    public static string base64Decode(string data)
    {
         byte[] toDecodeByte = Convert.FromBase64String(data);
    
         System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
         System.Text.Decoder utf8Decode = encoder.GetDecoder();
    
         int charCount = utf8Decode.GetCharCount(toDecodeByte, 0, toDecodeByte.Length);
    
         char[] decodedChar = new char[charCount];
         utf8Decode.GetChars(toDecodeByte, 0, toDecodeByte.Length, decodedChar, 0);
         string result = new String(decodedChar);
         return result;
    }
    
    0 讨论(0)
提交回复
热议问题