-1
我想通過使用下面的C#代碼一些串進行解碼:錯誤base64EncodeInvalid長度爲一個基地-64字符數組或字符串
public static string deCodeStdXML(string encodeXML)
{
string str2;
try
{
byte[] data = System.Convert.FromBase64String(encodeXML);
str2 = System.Text.ASCIIEncoding.ASCII.GetString(data);
}
catch (Exception exception)
{
throw new Exception("Error in base64Encode" + exception.Message);
}
return str2;
}
但是當我長的字符串進行測試,這是行不通的,見下圖:
string encodeXML = @"H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Il5mdbvM6/SLvGmyizx9lU/z4jKf/czLYpWXxTL/md/7i+f64ZNs8abOpm+L5cXPPH69+HKVL39yL323KJfNo3fN7LOP5m27enT37tXV1fjq3riqL";
string result = deCodeStdXML(encodeXML);
Console.WriteLine(result);
它拋出異常:
System.Exception was unhandled
HResult=-2146233088
Message=Error in base64EncodeInvalid length for a Base-64 char array or string.
Source=HarnessDBLayer
StackTrace:
at HarnessDBLayer.DBHelper.deCodeStdXML(String encodeXML) in c:\Users\zhanzhex\Documents\Visual Studio 2012\Projects\XMLValidation\HarnessDBLayer\DBLayer.cs:line 602
at ConsoleApplication1.Program.TEST_deCodeStdXML() in c:\Users\zhanzhex\Documents\Visual Studio 2012\Projects\XMLValidation\ConsoleApplication1\Program.cs:line 43
at ConsoleApplication1.Program.Main(String[] args) in c:\Users\zhanzhex\Documents\Visual Studio 2012\Projects\XMLValidation\ConsoleApplication1\Program.cs:line 22
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
有誰能夠告訴我如何解決這個問題?
那麼,這根本就不是一個有效的base64字符串。它有257個字符。它需要填充是有效的base64 ...你從哪裏開始的?順便說一句,你的try/catch塊比沒有意義的更糟糕 - 它將刪除可能有用的堆棧跟蹤。只是擺脫它,並將你的方法變成一種雙線法... –