Base64的字符串抛出无效字符错误 [英] Base64 String throwing invalid character error
问题描述
我不断收到以Base64无效字符的错误,即使我不应该。
I keep getting a Base64 invalid character error even though I shouldn't.
这个程序有一个XML文件,并将其导出到一个文件中。如果用户希望,它将玉米preSS的所述文件中。在COM pression工作正常,并返回一个字符串的Base64是连接codeD转换为UTF-8,并写入到一个文件中。
The program takes an XML file and exports it to a document. If the user wants, it will compress the file as well. The compression works fine and returns a Base64 String which is encoded into UTF-8 and written to a file.
当其时该文件加载到程序中我要检查其是否COM pressed与否,code是简单的:
When its time to reload the document into the program I have to check whether its compressed or not, the code is simply:
byte[] gzBuffer = System.Convert.FromBase64String(text);
return "1F-8B-08" == BitConverter.ToString(new List<Byte>(gzBuffer).GetRange(4, 3).ToArray());
它检查字符串的开头,看它是否具有的gzip code在里面。
It checks the beginning of the string to see if it has GZips code in it.
现在的事情是,我所有的测试工作。我有一个字符串,COM preSS它,DECOM preSS它,并将其与原来的。问题是,当我从ADO记录集返回的字符串。该字符串正是被写入文件(并在最后增加了一个\ 0,但我不认为,即使做任何事情,甚至修剪掉它仍然抛出)。我甚至复制并粘贴整个字符串到测试方法和COM preSS / DECOM preSS的。正常工作。
Now the thing is, all my tests work. I take a string, compress it, decompress it, and compare it to the original. The problem is when I get the string returned from an ADO Recordset. The string is exactly what was written to the file (with the addition of a "\0" at the end, but I don't think that even does anything, even trimmed off it still throws). I even copy and pasted the entire string into a test method and compress/decompress that. Works fine.
该测试将通过,但在code将使用完全相同的字符串失败?唯一的区别是刚刚宣布的规则字符串,并通过它,我得到一个从记录返回而不是。
The tests will pass but the code will fail using the exact same string? The only difference is instead of just declaring a regular string and passing it in I'm getting one returned from a recordset.
这是我在做什么任何想法错了?
Any ideas on what am I doing wrong?
推荐答案
您说
该字符串正是被写 到该文件(通过加入一 \ 0结尾,但我不认为 即使做任何事情)。
The string is exactly what was written to the file (with the addition of a "\0" at the end, but I don't think that even does anything).
在事实上,它确实做一些事情(它会导致你的code抛出一个出现FormatException
:无效字符一个base-64字符串),因为<一个href="http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx"><$c$c>Convert.FromBase64String$c$c>不考虑\ 0是一个有效的Base64字符
In fact, it does do something (it causes your code to throw a FormatException
:"Invalid character in a Base-64 string") because the Convert.FromBase64String
does not consider "\0" to be a valid Base64 character.
byte[] data1 = Convert.FromBase64String("AAAA\0"); // Throws exception
byte[] data2 = Convert.FromBase64String("AAAA"); // Works
解决方法:摆脱零终止(也许叫 .Trim(\ 0)
)
备注
借助 MSDN文档的 Convert.FromBase64String
说,这将抛出一个出现FormatException
在
s的长度,忽略空白 字符,不为零或者多 4。
The length of s, ignoring white space characters, is not zero or a multiple of 4.
- 或 -
s的格式无效。 s包含非基本64字符,更 两个以上的填充字符,或一个 在非空白字符 填充字符。
The format of s is invalid. s contains a non-base 64 character, more than two padding characters, or a non-white space character among the padding characters.
和
该基地64升序排列数字 从零是大写字符 A到Z,小写字母'A' 以Z,数字0至9,和 符号+和/'.
The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'.
这篇关于Base64的字符串抛出无效字符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!