目标是将带有一些字符的消息和一些值发送到另一个系统,它将读取该值,但我需要格式化其他系统的值以便能够读取它。
现在我有一些代码:
string Message = Convert.ToString({"x=" + x1 + "y=" + y1 + "idle=" + idle1 + "RT=" + RT + "LT=" + LT} );
这在某种程度上起作用x=x1
工作,使得值看起来像x=1
或x=65534
但是接收消息的系统需要读取字符串,我会像数组一样读取它,但这意味着值需要有固定的大小,所以我需要将x=1
格式化为{ {1}}而是。
我见过有人用" D6"作为参数,但我无法使用转换为字符串。
答案 0 :(得分:1)
你可以使用String.PadLeft(5,' 0')来获得5个字符
public string FormatVal(int value)
{
return value.ToString().PadLeft(5, '0');
}
然后
string Message = $"x = {FormatVal(x1)} y = {FormatVal(y1)}idle = {FormatVal(idle1)}RT = {FormatVal(RT)}LT = {FormatVal(LT)}";