字符串和数字互相转换,上位机几乎天天要用。

为什么需要互转?因为两边的「身份」不一样。

  • 界面输入框里,用户敲进 25.6,程序拿到的是字符串 string,不是数字,没法直接做加减乘除。
  • PLC 通过 Modbus 传来的温度是数字,但显示到屏幕上、写进日志里,又得变成字符串。

所以必须学会两个方向:数字变字符串、字符串变数字。

本篇目标:会用 ToString 把数字变字符串,会用 Parse、TryParse 把字符串变数字,知道字符串转数字最大的坑,能用 TryParse 写出不崩溃的代码。


一、数值转字符串(简单方向)

数字变字符串很简单,两个办法。

办法一:用加号拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program
{
static void Main()
{
double temp = 25.6;
int count = 100;

// 数字和字符串用 + 连,数字自动变成字符串
string s1 = "温度:" + temp;
string s2 = "数量:" + count;

Console.WriteLine(s1); // 温度:25.6
Console.WriteLine(s2); // 数量:100
}
}

办法二:用 ToString

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;

class Program
{
static void Main()
{
double temp = 25.6;
bool on = true;

string s1 = temp.ToString(); // "25.6"
string s2 = on.ToString(); // "True"

Console.WriteLine(s1); // 25.6
Console.WriteLine(s2); // True
}
}

数字变字符串几乎不会出错,方向简单。记住加号拼接和 ToString 两种就够。上面两种写法结果一样,加号拼接更常用于拼一句完整的话。


二、字符串转数值(重点方向,有坑)

字符串变数字麻烦得多,因为字符串可能不是合法数字。

反过来就危险了:字符串里装的不一定是数字。如果装的是 abc,硬要转成数字,程序就会崩溃。这是本篇最重要的坑。

2.1 用 Parse 转

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program
{
static void Main()
{
int a = int.Parse("123"); // 转 int,正常
double b = double.Parse("25.6"); // 转 double,正常

Console.WriteLine(a); // 123
Console.WriteLine(b); // 25.6
}
}
合法的字符串能顺利转

只要字符串里的内容长得像数字,Parse 就能转成功。int.Parse 要求是整数格式,double.Parse 允许带小数点。

2.2 坑:字符串不是数字会崩溃

1
2
3
4
5
6
7
8
9
10
using System;

class Program
{
static void Main()
{
int a = int.Parse("abc"); // 报错!abc 不是数字
Console.WriteLine(a);
}
}

上面代码一运行就崩溃,抛出一个 FormatException 异常,程序直接中断。原因就是 abc 不是合法数字。

上位机里最容易踩这个坑:界面上让用户输入温度,用户手滑输了 abc 或空内容,程序就崩了。所以直接用 Parse 是不安全的。

还有哪些会崩

下面这些用 int.Parse 都会崩:

  • 空字符串(用户啥都没输)
  • 带小数的数字(比如 25.6,因为 int 不接受小数)
  • 带空格、带字母、带特殊符号

所以字符串转数字,一定要防着「字符串不合法」这种情况。


三、用 TryParse 安全转换

TryParse 是 Parse 的安全版:转成功就返回 true 并给出结果,转失败就返回 false,不会崩溃。上位机里推荐用它。

TryParse 不会崩溃,这是它的最大优点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;

class Program
{
static void Main()
{
// 合法的情况
bool ok1 = int.TryParse("123", out int r1);
Console.WriteLine(ok1); // True
Console.WriteLine(r1); // 123

// 不合法的情况,不会崩溃
bool ok2 = int.TryParse("abc", out int r2);
Console.WriteLine(ok2); // False
Console.WriteLine(r2); // 0
}
}

TryParse 的写法是:int.TryParse(字符串, out int 结果变量)。它返回一个 bool,转成功是 true,失败是 false。后面的 out int r1 意思是「把转出来的结果放到 r1 这个变量里」。out 的详细用法后面学方法时再展开,现在先照着写。

TryParse 完整用法模板

把 TryParse 套进 if 里,就是最稳的写法:

1
2
3
4
5
6
7
8
9
10
string input = "25.6";   // 假设这是用户输入的

if (double.TryParse(input, out double result))
{
Console.WriteLine("转换成功:" + result);
}
else
{
Console.WriteLine("输入不合法,请重新输入");
}

转成功走 if 分支,失败走 else 分支,程序永远不崩。这就是上位机里处理用户输入的标准套路。


四、Parse 和 Convert 的区别

除了 Parse,还有个 Convert 也能转,很多人搞不清它俩的区别。简单说:功能差不多,但细节不同。

1
2
3
4
5
6
7
8
9
10
11
12
13
using System;

class Program
{
static void Main()
{
int a = int.Parse("123"); // Parse
int b = Convert.ToInt32("123"); // Convert

Console.WriteLine(a); // 123
Console.WriteLine(b); // 123
}
}
写法 说明 特殊点
int.Parse(字符串) 转成 int 字符串为 null 或空会崩
Convert.ToInt32(字符串) 转成 int 字符串为 null 会返回 0

日常字符串转数字,用 TryParse 最安全。Convert 和 Parse 在没有把握字符串一定合法时,都不建议直接用。


五、上位机实战:处理界面输入和报文

两个最真实的场景,把前面学的串起来。

场景一:界面输入温度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

class Program
{
static void Main()
{
// 假设用户在输入框里敲了一个温度值
string input = "36.5";

// 用 TryParse 安全转成数字
if (double.TryParse(input, out double temp))
{
// 转成功,接着做判断
string state = temp > 80.0 ? "温度过高" : "温度正常";
Console.WriteLine(state); // 温度正常
}
else
{
Console.WriteLine("请输入正确的数字");
}
}
}

场景二:解析 Modbus 报文里的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;

class Program
{
static void Main()
{
// 假设从串口收到的一帧报文,解析出其中一段数字字符串
string valueStr = "13824";

// 转成 int 原始值,再做模拟量换算
if (int.TryParse(valueStr, out int raw))
{
double real = raw / 27648.0 * 100.0;
Console.WriteLine("实际温度:" + real); // 实际温度:50
}
else
{
Console.WriteLine("报文数据不合法");
}
}
}

两个场景用了同一套套路:先 TryParse 判断能不能转,能转再做后续运算,不能转就提示错误。这样无论用户怎么乱输、报文怎么异常,程序都不会崩。


六、小结

这一篇我们学会了两类转换:

  • 数值变字符串:加号拼接、ToString,简单不会错。
  • 字符串变数值:Parse 会崩、TryParse 安全、Convert 也能转。

口诀:

数字变字符,拼接或 ToString;字符变数字,首选 TryParse 保平安。

会用加号和 ToString 把数字变字符串

会用 int.Parse、double.Parse 把字符串变数字

知道字符串不是数字时 Parse 会崩溃

会用 TryParse 写出不崩溃的转换


七、能跑起来的小练习

点我看完整练习代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;

class Program
{
static void Main()
{
// 1) 数值转字符串
double temp = 25.6;
string s = "温度:" + temp;
Console.WriteLine(s); // 温度:25.6

// 2) 字符串转数值(安全版)
string ok = "123";
if (int.TryParse(ok, out int a))
{
Console.WriteLine(a); // 123
}

// 3) 字符串转数值(非法输入不崩溃)
string bad = "abc";
if (int.TryParse(bad, out int b))
{
Console.WriteLine(b);
}
else
{
Console.WriteLine("转换失败"); // 转换失败
}
}
}