相关疑难解决方法(0)

如何从Powershell中的字符串转换为UInt64?字符串到数字的转换

请考虑以下Powershell代码段:

[Uint64] $Memory = 1GB
[string] $MemoryFromString = "1GB"
[Uint64] $ConvertedMemory = [Convert]::ToUInt64($MemoryFromString)
Run Code Online (Sandbox Code Playgroud)

第3行失败了:

Exception calling "ToUInt64" with "1" argument(s): "Input string was not in a correct format."
At line:1 char:1
+ [Uint64]$ConvertedMemory = [Convert]::ToUInt64($MemoryFromString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException
Run Code Online (Sandbox Code Playgroud)

如果我查看以下内容$Memory:

PS C:\> $Memory
1073741824
Run Code Online (Sandbox Code Playgroud)

这很好.

那么,如何在Powershell中将值"1GB"从字符串转换为UInt64?

powershell type-conversion powershell-5.0

3
推荐指数
2
解决办法
3976
查看次数