C# 字符串长度不能为零。参数名称:oldValue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16733845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String cannot be of zero length. Parameter name: oldValue
提问by Raheel
I am working on Decrypt Password and I stuck on this error:
我正在处理解密密码,但遇到了这个错误:
String cannot be of zero length. Parameter name: oldValue
字符串长度不能为零。参数名称:oldValue
Kindly help on this error or suggest me another program for decryption.
请帮助解决此错误或建议我使用另一个解密程序。
Here is the full code:
这是完整的代码:
string decryptpwd = string.Empty;
UTF8Encoding encodepwd = new UTF8Encoding();
Decoder Decode = encodepwd.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));
int charcount = Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decode_char = new char[charcount];
Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decode_char, 0);
decryptpwd = new String(decode_char);
return decryptpwd;
回答by Darren
encryptpwd.Replace("","+")
What exactly are you replacing? You haven't specified an original value to be replaced.
你究竟要更换什么?您尚未指定要替换的原始值。
String.Replacetakes two string arguments oldValueand newValue. You specified the newValue +however an empty string is not legal for the oldValue.
String.Replace接受两个字符串参数oldValue和newValue。您指定了 newValue+但是空字符串对于oldValue.
Therefore if you want to replace a blank space with +try:
因此,如果你想用+try替换一个空格:
encryptpwd.Replace(" ","+");
Or vice versa:
或相反亦然:
encryptpwd.Replace("+"," ");
回答by Rajeev Kumar
problem is here
问题在这里
encryptpwd.Replace("","+")
Should have some character or string to replace
应该有一些字符或字符串来替换
encryptpwd.Replace(" ","+")
回答by Steve
You are asking the Replace method to change an empty string (first parameter) with a plus character (second parameter). This make no sense and Replace is complaining about this.
I think you want to do the reverse
您要求 Replace 方法更改带有加号字符(第二个参数)的空字符串(第一个参数)。这毫无意义,Replace 正在抱怨这一点。
我想你想做相反的事情
byte[] todecode_byte = Convert.FromBase64String(encryptpwd.Replace("+",""));
A part from this I am not sure what the result will be when you change something into the input string and apply a FromBase64String to the result. Well, it really depends on what was originally in the string, but for sure (if encryptpwdis really a Base64 string) there are no spaces to replace.
其中的一部分我不确定当您将某些内容更改为输入字符串并将 FromBase64String 应用于结果时的结果。嗯,这真的取决于字符串中的原始内容,但是可以肯定(如果encryptpwd确实是 Base64 字符串)没有空格可以替换。
Keep in mind that you can't pass a normal string to Convert.FromBase64String, you need a string that is a base 64 string
请记住,您不能将普通字符串传递给 Convert.FromBase64String,您需要一个基数为 64 的字符串
For example
例如
string pwd = "786"; // The original string
UnicodeEncoding u = new UnicodeEncoding();
byte[] x = u.GetBytes(pwd); // The Unicode bytes of the string above
// Convert bytes to a base64 string
string b64 = Convert.ToBase64String(x);
Console.WriteLine(b64);
// Go back to the plain text string
byte[] b = Convert.FromBase64String(b64);
string result = u.GetString(b);
Console.WriteLine(result);
A final word. Someone (@Slacks) already tells you that a base64 string is not an encryption technology and you shouldn't use it for cryptingpasswords (They are not crypted at all)
最后一句话。有人(@Slacks)已经告诉你,base64 字符串不是加密技术,你不应该用它来加密密码(它们根本没有加密)

