简体   繁体   English

从base64string到string回到base64string

[英]base64string to string back to base64string

I am trying an experiment to convert a base64string to a string then back to a base64string, however, I am not getting my original base64string: 我正在尝试将base64string转换为字符串然后再转换回base64string的实验,但是,我没有得到原始的base64string:

String profilepic = "/9j/4AAQ";

string Orig = System.Text.Encoding.Unicode.GetString(Convert.FromBase64String(profilepic));

string New = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(Orig));

The string New returns "/f//4AAQ". 字符串New返回“ / f // 4AAQ”。

Any thoughts of why this is happening? 为什么会这样呢?

You are doing it wrong. 你做错了。 You should do it as below: 您应该按照以下步骤进行操作:

namespace ConsoleApplication1
{
    using System;
    using System.Text;

    class Program
    {
        static void Main(string[] args)
        {
            string profilepic = "/9j/4AAQ";
            string New = Convert.ToBase64String(Encoding.Unicode.GetBytes(profilepic));
            byte[] raw = Convert.FromBase64String(New); // unpack the base-64 to a blob
            string s = Encoding.Unicode.GetString(raw); // outputs /9j/4AAQ
            Console.ReadKey();
        }
    }
}

You're assuming that the base64-encoded binary data in your example contains a UTF-16 encoded message. 您假设示例中以base64编码的二进制数据包含UTF-16编码的消息。 This may simply not be the case, and the System.Text.Encoding.Unicode class may alter the contents by discarding the bytes that it doesn't understand. 事实并非如此, System.Text.Encoding.Unicode类可能会通过丢弃不了解的字节来更改内容。

Therefore, the result of base64-encoding the UTF-16 encoded byte stream of the returned string may not yield the same output. 因此,对返回的字符串的UTF-16编码的字节流进行base64编码的结果可能不会产生相同的输出。

Your input string contains the binary sequence 0xff 0xd8 0xff 0xe0 0x00 0x10 (in hex). 您的输入字符串包含二进制序列0xff 0xd8 0xff 0xe0 0x00 0x10(以十六进制表示)。 Interpreting this as UTF-16LE (which you're using with System.Text.Encoding.Unicode ) the first character would be 0xffd8, but is placed in the string as 0xfffd, which explains the change. 将此解释为UTF-16LE(与System.Text.Encoding.Unicode使用),第一个字符将是0xffd8,但会以0xfffd的形式放置在字符串中,这说明了更改。

I tried decoding it with Encoding.Unicode , Encoding.UTF8 and Encoding.Default , but none of them yielded anything intelligible. 我尝试使用Encoding.UnicodeEncoding.UTF8Encoding.Default对其进行解码,但是它们都不产生任何可理解的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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