将 .net String 对象转换为 base64 编码的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2632215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:09:38  来源:igfitidea点击:

Convert .net String object into base64 encoded string

.netstringencodingbase64

提问by chester89

I have a question, which Unicode encoding to use while encoding .NET string into base64? I know strings are UTF-16 encoded on Windows, so is my way of encoding is the right one?

我有一个问题,在将 .NET 字符串编码为 base64 时使用哪种 Unicode 编码?我知道字符串在 Windows 上是 UTF-16 编码的,所以我的编码方式是正确的吗?

public static String ToBase64String(this String source) {
        return Convert.ToBase64String(Encoding.Unicode.GetBytes(source));
    }

采纳答案by Adam Robinson

What you've provided is perfectly functional. It will produce a base64-encoded string of the bytes of your source string encoded in UTF-16.

您提供的功能非常实用。它将生成以 UTF-16 编码的源字符串字节的 base64 编码字符串。

If you're asking if UTF-16 can represent any character in your string, then yes. The only difference between UTF-16 and UTF-32 is that UTF-16 is a variable-length encoding; it uses two-bytes to represent characters within a subset, and four-bytes for all other characters.

如果您问 UTF-16 是否可以表示字符串中的任何字符,那么可以。UTF-16 和 UTF-32 的唯一区别是 UTF-16 是可变长度编码;它使用两个字节来表示子集中的字符,使用四字节来表示所有其他字符。

There are no unicode characters that cannot be represented by UTF-16.

没有不能用 UTF-16 表示的 unicode 字符。

回答by Alan Moore

Be aware that you don't haveto use UTF-16 just because that's what .NET strings use. When you create that byte array, you're free to choose any encoding that will handle all the characters in your string. For example, UTF-8 would be more efficient if the text is in a Latin-based language, but it can still handle every known character.

请注意,您不必仅仅因为 .NET 字符串使用 UTF-16 就必须使用 UTF-16。创建该字节数组时,您可以自由选择将处理字符串中所有字符的任何编码。例如,如果文本是基于拉丁语的语言,UTF-8 会更有效,但它仍然可以处理每个已知字符。

The most important concern is that whatever software decodes the base64 string, needs to know which encoding to apply to the byte array to re-create the original string.

最重要的问题是,无论使用什么软件解码 base64 字符串,都需要知道将哪种编码应用于字节数组以重新创建原始字符串。

回答by DareDevil

Here is the Solution, I have converted a Random string conversion like you can give any size up to 10 that Base64 will output.

这是解决方案,我已经转换了一个 Random 字符串转换,就像您可以提供 Base64 将输出的最大 10 的任何大小。

//This function will return a random string from the given numeric characters
public string RandomString(int size)
{
const string legalCharacters = "1234567890";
Random random = new Random();
StringBuilder builder = new StringBuilder();
char ch = '##代码##';

for (int i = 0; i <= size - 1; i++) {
    ch = legalCharacters(random.Next(0, legalCharacters.Length));
    builder.Append(ch);
}
return builder.ToString();
}
public const string BASE64 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
public string DecToBase64(long lVal)
{
string sVal = null;
sVal = "";
while (lVal >= 64) {
    sVal = sVal + DecToBase64(lVal / 64);
    lVal = lVal - 64 * (lVal / 64);
}
sVal = sVal + Strings.Mid(BASE64, Convert.ToInt32(lVal) + 1, 1);
return sVal;
}

//here is how we can have result in variable:
string Base64 = "";
Base64 = DecToBase64(RandomString(10)); //this will produce a combination up-to length of 10

回答by abatishchev

MSDNconfirms that UnicodeEncodingclass represents a UTF-16encoding of Unicode characters.

MSDN确认UnicodeEncoding该类表示UTF-16Unicode 字符的编码。