【问题标题】:C# Memory.WriteFloat() Cannot convert string to long [duplicate]C#Memory.WriteFloat()无法将字符串转换为长[重复]
【发布时间】: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;
                }

【问题讨论】:

标签: c# memory


【解决方案1】:

你需要 Int64.Parse

     string s="1234";
    long l = System.Int64.Parse(s);

或者,如果您不确定字符串是否包含有效数字,请使用

bool success = Int64.TryParse(s, out long l);

【讨论】:

  • 或者只是long.Parse/long.TryParse。不管你喜欢哪个,真的。
  • @pm100 我收到错误 ************** 异常文本 ************** System.FormatException: Input string格式不正确。在 System.Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type) at System.Number.ParseInt64(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info) at System.Int64.Parse(String s)
  • @pm100 使用代码 bool success = Int64.TryParse(s, out long l);可以正常工作,没有错误。但是当我更改内存时不会更新值。WriteFloat
  • 发布您调用 tryparse 的代码(编辑问题)
  • @newcsharpcoder 读取the documentation 可以节省大量时间。虽然 TryParse 没有抛出异常,但它无法“工作”转换不可解析的字符串值。结果将是false,如果转换失败,out 值将设置为 0:“如果 s 参数为 null 或 Empty、格式不正确或表示小于 MinValue 的数字,则转换失败或大于 MaxValue。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 1970-01-01
  • 2023-03-28
  • 2014-01-19
  • 2017-08-17
  • 2012-04-13
  • 2012-07-30
相关资源
最近更新 更多