【发布时间】:2022-01-29 22:26:54
【问题描述】:
有问题的代码行:
memory.WriteFloat(tp_x, this.X)
this.X 很好,可以正常工作。 对于值 tp_x,它是一个字符串。 我从文本文件中获取它并将其解析为精确值。我曾多次尝试将此字符串转换为 Int32/64、float、long 等。但是 memory.WriteFloat() 并没有出错。 Picture of code
我试过用
memory.ReadFloat(tp_X)
但我得到了同样的错误。 无法从“字符串”转换为“长”。我尝试了很多不同的代码来转换和使用它,但没有任何效果或改变,所以我在这里问一个问题。如果我不知道,我不会问。谢谢,请告诉我!
为 pm100 编辑 这是我的新代码: Picture 1 Where I get my error Picture 2 因此,使用图片中的代码@pm100,我没有遇到任何问题,并且可以完美编译。虽然,当我执行这个我得到错误
我收到错误 System.FormatException:输入字符串的格式不正确。 在 System.Number.ThrowOverflowOrFormatException(ParsingStatus 状态,TypeCode 类型) 在 System.Number.ParseInt64(ReadOnlySpan`1 值,NumberStyles 样式,NumberFormatInfo 信息) 在 System.Int64.Parse(String s)
我的代码: 整数计数器 = 0; // location 是用户选中的组合框的选中索引
foreach (string line in System.IO.File.ReadLines(@"path"))
{
if (counter == location)
{
string[] words = line.Split(",");
int tempcounter = 1;
string tp_x = "";
string tp_y = "";
string tp_z = "";
foreach (var word in words)
{
if (tempcounter == 1)
{
tp_x += word;
}
if (tempcounter == 2)
{
tp_y += word;
}
if (tempcounter == 3)
{
tp_z += word;
}
tempcounter += 1;
//everytime works perfect above, now below
long l = System.Int64.Parse(tp_x);
long l2 = System.Int64.Parse(tp_y);
long l3 = System.Int64.Parse(tp_z);
tp_xx = l;
tp_yy = l2;
tp_zz = l3;
for (int i = 0; i < address.vz.Length; i++)
{
mem.WriteFloat(tp_xx, this.X);
mem.WriteFloat(tp_yy, this.Y);
mem.WriteFloat(tp_zz, this.Z);
}
}
return false;
}
counter += 1;
}
【问题讨论】:
-
“tp_x,它是一个
string”;所以将其转换为long,或作为搜索短语“C# 如何将字符串转换为长字符串”。 -
也许 tp_x 应该已经是
long(即long tp_x;),任何转换都在分配之前发生。那么 tp_x 的值就不需要转换为使用站点了。 -
@user2864740 试了几次。我在网上尝试了很多不同的东西来转换等等。没有运气! :)
-
@newcaharpercoder 然后尝试被错误地应用。鉴于缺乏明确的最小再现示例,尝试提供更多输入是不切实际的。见stackoverflow.com/help/minimal-reproducible-example