Convert.ToBase64String()编码
(2013-05-20 15:22:31)
标签:
杂谈 |
分类: C# |
在使用Convert.ToBase64String()对字符串进行Base64编码时,注意的几点:
例:string s
= "Hello";
byte[]
bytes = Convert.FromBase64String(s);
以上代码在运行时会抛出FormatException异常.提示为:Base-64字符数组的无效长度
原因:当Convert.FromBase64String方法的参数s的长度小于 4 或不是 4 的偶数倍时,将会抛出FormatException。
例:
Convert.FromBase64String("Hell");
// Normal.
Convert.FromBase64String("Hell
");
// Normal.(忽略空格)
Convert.FromBase64String("Hello!");
// throw FormatException.
Convert.FromBase64String("Hello Net"); // Normal.(忽略空格)
以上代码在运行时会抛出FormatException异常.提示为:Base-64字符数组的无效长度
原因:当Convert.FromBase64String方法的参数s的长度小于 4 或不是 4 的偶数倍时,将会抛出FormatException。