[英]Base 64 encoding and Decoding
I want to decode special characters in Base 64 and I want to include space into it. 我想在Base 64中解码特殊字符,并且要在其中包含空格。 Please tell me how to handle space in base 64 encoding and decoding.
请告诉我如何在base 64编码和解码中处理空间。
<add key="SpecialCharacter" value="w6J8YSzDoXxhLMOlfGEsw6R8YSzDo3xhLMOmfGFlLMSNfGMsw6l8ZSzDqHxlLMOqfGUsw6t8ZSzDu3x1LMO6fHUsw7x8dSzDuHxvLMOzfG8sw7R8byzDtnxvLMOyfG8sxaF8byxgfCwnfCzFgnxsLMW8fHo="/>
Take a look into these functions: 看一下这些功能:
public static string ToBase64(this string value)
{
byte[] bytes = Encoding.Default.GetBytes(value);
return Convert.ToBase64String(bytes);
}
public static string FromBase64(this string value)
{
byte[] bytes = Convert.FromBase64String(value);
return Encoding.Default.GetString(bytes);
}
The first one converts a string to a base 64 string. 第一个将字符串转换为基本64字符串。
eg: string base64 = "Hello World!".ToBase64()
例如:
string base64 = "Hello World!".ToBase64()
The second one returns it to a 'normal' string: string original = base64.FromBase64()
第二个返回到“正常”字符串:
string original = base64.FromBase64()
The core functionality is in Convert.FromBase64String(string value)
. 核心功能在
Convert.FromBase64String(string value)
。 It returns a byte array, which has to converted to a string with Encoding
. 它返回一个字节数组,必须使用
Encoding
将其转换为字符串。 When you know the used encoding, you shuld use it and not the default (UTF-16, i think). 当您知道使用的编码时,应该使用它而不是默认的编码(我认为是UTF-16)。
I tested some encodings. 我测试了一些编码。 In you case, the string is encoded in UTF8 and results in:
在这种情况下,该字符串以UTF8编码,结果为:
â|a,á|a,å|a,ä|a,ã|a,æ|ae,č|c,é|e,è|e,ê|e,ë|e,û|u,ú|u,ü|u,ø|o,ó|o,ô|o,ö|o,ò|o,š|o,`|,'|,ł|l,ż|z
A | A,A | A,A | A,A | A,A | A,æ| AE,č| C,E | E,E | E,E | E,E | E,U | U,U | U,U | U,直径| O,O- | O,O- | O,O- | O,O- | O,S | O,`|“|,L | L,Z | Z
In addition to the comment to @WDS here again: Spaces should get converted to base64, too. 除了在这里再次评论@WDS:空间也应该转换为base64。 No need for spacial handing them.
无需空间传递它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.