十六进制到十进制转换-PowerShell 5 [英] Hex to Decimal Conversion - PowerShell 5

查看:71
本文介绍了十六进制到十进制转换-PowerShell 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当在PowerShell中将某些值从十六进制转换为十进制时,我得到了不正确的值.

For some reason, when converting some values in PowerShell from Hex to Decimal, I get incorrect value.

例如:-键入 0xC0000000 ,然后按Enter,您将得到 -1073741824 ,这是不正确的,正确的值为 3221225472 .给出的答案是我输入的负十六进制数的值 FFFFFFFFC0000000

For example: - Type 0xC0000000 then hit enter, you'll get -1073741824 which is incorrect, the right value is 3221225472. The answer given is the value for the negative Hex number I entered FFFFFFFFC0000000

我认为这可能是一个解释问题,所以我明确要求将值从十六进制转换为Base10(十进制),我仍然得到相同的错误答案 -1073741824 :

I thought this could be an interpretation issue so I explicitly asked to convert the value to from Hex to Base10 (Decimal), I still got the same incorrect answer -1073741824:

[Convert]::ToString(0xc0000000,10)

PowerShell为什么将 0xc0000000 解释为十六进制数字 c0000000 的负数?我在这里想念东西吗?

Why does PowerShell interpret 0xc0000000 as the negative for the Hex number c0000000? am I missing something here?

推荐答案

这是PowerShell陷阱(也存在于早期版本中).当您实际上希望该值是无符号整数时,该值将被解释为有符号整数.您需要像这样进行转换才能解决此问题:

That's a PowerShell gotcha (exists in earlier versions too). The value is interpreted as a signed integer when you actually want it to be an unsigned integer. You need to do the conversion like this to get around the issue:

[uint32]"0xC0000000"

这篇关于十六进制到十进制转换-PowerShell 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆