string str = "This is a string"; byte[] b = System.Text.Encoding.Default.GetBytes( str );string base64Str = Convert.ToBase64String( b );//专成Base64编码的字符串b = Convert.FromBase64String( base64Str );string str1 = System.Text.Encoding.Default.GetString( b );//转回来
我这样写了,但总是提示"Base 64 string length is not 4 or is not an even multiple of 4.";难道传入的字串必须是4或是4的倍数个字符吗? public string test(string str) { string s = FromBase64(str); s += " OK!"; return ToBase64(s); }private string ToBase64( string s ) { byte[] b = System.Text.Encoding.Default.GetBytes( s ); return Convert.ToBase64String( b ); }private string FromBase64( string s ) { byte[] b; try { b = Convert.FromBase64String( s ); return System.Text.Encoding.Default.GetString( b ); } catch (System.ArgumentNullException) { return "Base 64 string is null."; } catch (System.FormatException) { return "Base 64 string length is not " + "4 or is not an even multiple of 4."; } }
byte[] b = System.Text.Encoding.Default.GetBytes( str );string base64Str = Convert.ToBase64String( b );//专成Base64编码的字符串b = Convert.FromBase64String( base64Str );string str1 = System.Text.Encoding.Default.GetString( b );//转回来
public string test(string str)
{
string s = FromBase64(str);
s += " OK!";
return ToBase64(s);
}private string ToBase64( string s )
{
byte[] b = System.Text.Encoding.Default.GetBytes( s );
return Convert.ToBase64String( b );
}private string FromBase64( string s )
{
byte[] b;
try
{
b = Convert.FromBase64String( s );
return System.Text.Encoding.Default.GetString( b );
}
catch (System.ArgumentNullException)
{
return "Base 64 string is null.";
}
catch (System.FormatException)
{
return "Base 64 string length is not " +
"4 or is not an even multiple of 4.";
}
}