将“字符串对象”转换为Int32的数组,然后转换为Uint32的数组

sta*_*eUp 0 c# arrays string casting

我有以下对象,其中包含一个希望转换为Int32 []数组的字符串:

object stringObject= "4194304 4286147 4380001 4475911 4573920 4674076 4776425 4881015 4987896 5097116 5208729 5322785 5439339 5558445 5680159 5804538 5931641 6061527 6194257 6329894 6468501 6610142 6754886 6902798 7053950 7208411 7366255 7527555 7692387 7860828 8032958 8208857 8761011 9122297 9473110 9814040 10145628 10468373 10782733 11089135 11387970 11679603 11964374 12242598 12514569 12780563 13040835 13295628 13545167 13789664 14029319 14264321 14494846 14721062 14943127 15161191 15375395 15585873 15792753 15996157 16196198 16392986 16586625 16777215"
Run Code Online (Sandbox Code Playgroud)

我做了什么(不确定这是最好的方法吗?):

int[] retValues = stringObject.Split(' ').Select(v => Convert.ToInt32(v, 10)).ToArray();
Run Code Online (Sandbox Code Playgroud)

没问题,但是接下来如何将这些retValues转换为Uint32 []数组。我尝试了以下操作,但不起作用:

uint[] retValuesUint = retValues.Select(v => Convert.ToUInt32(v, 10));
Run Code Online (Sandbox Code Playgroud)

要么

uint[] retValuesUint = retValues.Select(v => (UInt32)v);
Run Code Online (Sandbox Code Playgroud)

V0l*_*dek 7

首先,由于object没有Split方法,因此样本无法编译。

其次,如果将的声明更改stringObject为type stringretValuesints是其中包含64 ints的s 数组。它的长度是64,里面的对象是still int,这是的别名Int32

Console.WriteLine(retValues.Length);
Run Code Online (Sandbox Code Playgroud)
> 64
Run Code Online (Sandbox Code Playgroud)
Console.WriteLine(retValues[0].GetType().Name);
Run Code Online (Sandbox Code Playgroud)
> Int32
Run Code Online (Sandbox Code Playgroud)

编辑:

uint您提供的转换有两个问题。

  1. ToUInt32不会像接受int fromBase参数那样接受参数ToInt32-仅适用于base10数字。

  2. 您已经忘记了ToArray通话的最后,实际上是uint[]您想要的。

uint[] retValuesUint = retValues.Select(v => Convert.ToUInt32(v)).ToArray();
Run Code Online (Sandbox Code Playgroud)