[英]Trying to convert a string to integer causes a error of type System.FormatException
I am making a client for my server and I am stuck. 我正在为我的服务器制作一个客户端而且我被卡住了。 My server is sending Unicode text, and my client recieves it.
我的服务器正在发送Unicode文本,我的客户端收到它。 The problem is this piece of code:
问题是这段代码:
Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim()))
I am trying to get the text from my Enum FTPCommands
with the number the server sent me. 我试图从我的Enum
FTPCommands
获取文件, FTPCommands
包含服务器发送给我的号码。 First my client is recieving the ID: 1 732847823
(Result from the debugger). 首先我的客户端收到ID:
1 732847823
(来自调试器的结果)。
It splits correctly, in this case: replace[0].ToString()
-->"1" AND replace[1].ToString()
-->"732847823" 它正确分割,在这种情况下:
replace[0].ToString()
- >“1”并replace[1].ToString()
- >“732847823”
No error occurs! 没有错误发生!
On the second command, the client is recieving an error from the server: 在第二个命令,客户端从服务器收到错误:
8 Server is too full, try again laiter
And it also splits correctly, in this case: 它也正确分裂,在这种情况下:
replace[0].ToString()
-->"1" AND replace[0].ToString()
-->"8" AND replace[1].ToString()
-->"Server" AND replace[2].ToString()
-->"is" AND replace[3].ToString()
-->"too" AND replace[4].ToString()
-->"full," AND replace[4].ToString()
-->"try" AND replace[4].ToString()
-->"again" AND replace[4].ToString()
-->"laiter" replace[0].ToString()
- >“1”并replace[0].ToString()
- >“8”并replace[1].ToString()
- >“Server”并replace[2].ToString()
- >“is”AND replace[3].ToString()
- >“too”AND replace[4].ToString()
- >“full”,AND replace[4].ToString()
- - >“try”并replace[4].ToString()
- >“再次”并replace[4].ToString()
- >“laiter”
And now the error occurs! 现在错误发生了! I dont know why this is working with the first command and not also with the command
8
. 我不知道为什么这是使用第一个命令而不是命令
8
。 If I replace the replace[0].Trim()
with the number 8 it works fine. 如果我用数字8替换
replace[0].Trim()
,它可以正常工作。 What is wrong with the code, or what could cause that error? 代码有什么问题,或者什么可能导致错误? Is it because I am using Unicode?
是因为我使用的是Unicode吗?
Here are some samples of my code: 以下是我的代码的一些示例:
NetworkStream stream = client.GetStream();
StreamWriter sw = new StreamWriter(stream, Encoding.Unicode);
StreamReader sr = new StreamReader(stream);
//SendFileToServer(@"\\fs1\home\manuel.boehmler\contacts.rar", client);
AnswerToServer(FTPCommands.ID,null,sw);
AnswerToServer(FTPCommands.Username, txtBUserName.Text, sw);
AnswerToServer(FTPCommands.Password, txtBPassword.Text, sw);
AnswerToServer(FTPCommands.login,null, sw);
AnswerToServer(FTPCommands.MaxMbytes, txtBPassword.Text, sw);
//AnswerToServer(FTPCommands.FileTransfer,"", sw);
string line = null;
while (client.Connected == true)
{
if (!string.IsNullOrEmpty(line = sr.ReadLine()) && line.Length >= 2)
{
RichTxtMessage("Bekommt: " + line, true);
string[] replace = null;
string command = "";
replace = line.Split(' ');
command = replace[0].ToUpperInvariant();
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : null;
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim())).ToUpperInvariant()) //Error occurs here
{
case "ID":
RichTxtMessage(arguments,true);
break;
case "ERROR":
RichTxtMessage(arguments, true);
break;
}
}
public enum FTPCommands
{
Persmission = 0,
ID = 1,
Username = 2,
Password = 3,
UserNameReq = 4,
PasswordReq = 5,
Chat = 6,
MessageRecived = 7,
Error = 8,
Warning = 9
}
EDIT (UPDATE): 编辑(更新):
When Iam using UTF8 as encoding the console gives me this result back: 当Iam使用UTF8作为编码时,控制台会给我这个结果:
As you can see the wired symbols as first char. 正如您可以看到有线符号作为第一个字符。 However it doesnt show me the symboles when Iam using UNICODE (but still not working).
然而,当Iam使用UNICODE时,它并没有向我显示符号(但仍然无效)。 I solved the problem, I dont use any Encoding for my FTP-Server
我解决了这个问题,我不使用任何编码为我的FTP服务器
Instead of Convert.ToInt32(replace[0].Trim())) use int.TryParse: 而不是Convert.ToInt32(替换[0] .Trim()))使用int.TryParse:
int i = 0;
int.TryParse(replace[0].Trim(), out i)
Edit: If null was in an element, that element cannot be converted as int and it will throw error 编辑:如果null在元素中,则该元素不能转换为int,它将抛出错误
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : null;
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(replace[0].Trim())).ToUpperInvariant()) //Error occurs here
Assign some int instead of null, or check it is not null or empty before converting to int 分配一些int而不是null,或者在转换为int之前检查它是否为null或为空
string arguments = replace.Length > 1 ? line.Substring(replace[0].Length + 1) : "-1";
// OR do this
int opt = 0;
if (!string.IsNullOrEmpty(replace[0].Trim())
opt = int.TryParse(replace[0].Trim(), out opt);
switch (Enum.GetName(typeof(FTPCommands), Convert.ToInt32(opt)).ToUpperInvariant()) //Error occurs here
I am not sure why you are bothering to convert the integer to a string. 我不知道为什么你很难将整数转换为字符串。 You could just cast to the enum and use switch with just enum values, eg
您可以直接转换为枚举并使用仅包含枚举值的开关,例如
int id = -1;
if (int.TryParse (replace[0].Trim (), id))
{
switch ((FTPCommands)id)
{
case FTPCommands.ID:
RichTxtMessage(arguments,true);
break;
case FTPCommands.Error:
RichTxtMessage(arguments, true);
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.