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 1356
逝去的感伤
逝去的感伤 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: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;
    }
    

提交回复
热议问题