<iframe marginwidth="0" marginheight="0" src="http://218.16.120.35:65001/PC/Global/images/b.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
在使用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.(忽略空格)
本文介绍了在使用Convert.ToBase64String方法时可能遇到的FormatException异常情况及其原因。当输入字符串长度不是4的倍数时,该方法将无法正确解析Base64编码。
8877

被折叠的 条评论
为什么被折叠?



