[英]Concatenate strings, convert to ushort, compare against ushort
So I have some constants: 所以我有一些常数:
const ushort _WIN32_WINNT_NT4 = 0x0400;
const ushort _WIN32_WINNT_WIN2K = 0x0500;
....
And then I have a major version number, minor version number, and service pack number, that, when you concatenate those together, it's the same as the number above - except 2 of them are int
and one is a string
. 然后我有一个主要版本号,次要版本号和Service Pack编号,当您将它们串联在一起时,它与上面的编号相同-除了它们中的2个是int
且一个是string
。 I can get them all into a string like this: 我可以将它们全部放入这样的字符串中:
string version = majorVersion.ToString() + minorVersion.ToString() + sp;
For Windows 2000, this would look like "500"
. 对于Windows 2000,这看起来像是"500"
。 It "matches" the ushort, just without the 0x0
. 它“匹配” ushort,只是没有0x0
。
What I'd like to do is hand off version
to a function, as a ushort
that returns the correct OS: 我想做的是将version
切换到函数,作为返回正确OS的ushort
:
private static string WindowsVersion(ushort uniNum)
{
switch (uniNum)
{
case _WIN32_WINNT_NT4:
return "Windows NT 4.0";
case _WIN32_WINNT_WIN2K:
return "Windows 2000";
....
default:
return "Unknown OS version.";
}
}
The problem is, even if I do: 问题是,即使我这样做:
ushort uniNum = Convert.ToUInt16(version);
And say it sends it in as 500
, the constant is 0x0500
, so it never finds the OS and returns Unknown OS version
, instead. 并说它以500
发送,常量是0x0500
,所以它从不找到Unknown OS version
,而是返回Unknown OS version
。 When I debug and hover over _WIN32_WINNT_WIN2K
, it's actually 1280
in decimal format. 当我调试并悬停在_WIN32_WINNT_WIN2K
,实际上是1280
(十进制格式)。 _WIN32_WINNT_NT4
is showing as 1024
, so "400"
would never match it. _WIN32_WINNT_NT4
显示为1024
,因此"400"
将永远不匹配。
And if I include the "0x0": 如果我包含“ 0x0”:
ushort uniNum = Convert.ToUInt16("0x0" + version);
It gives me an error that the input is in the incorrect format. 它给我一个错误,就是输入格式不正确。
I'm probably missing something simple, but I can't find anything anywhere that's been helpful. 我可能缺少一些简单的东西,但是在任何有用的地方都找不到。
You already have the constants and they are hexadecimal. 您已经有了常量,并且它们是十六进制的。 If you are getting 400
and 500
they are also hexadecimal, so replace: 如果您得到400
和500
它们也都是十六进制的,因此请替换:
ushort uniNum = Convert.ToUInt16(version);
with: 有:
ushort uniNum = Convert.ToUInt16(version, 16);
Your constant declaration's value is a hexadecimal literal expression: 常量声明的值是一个十六进制文字表达式:
const ushort _WIN32_WINNT_NT4 = 0x0400;
Where 0x0400
is equivalent to hex 400
, decimal 1024
. 其中0x0400
等于十六进制400
,十进制1024
。 So basically, you're not comparing to 400 but to 1024. 所以基本上,您所比较的不是400,而是1024。
Change your constant to be 400 if you'd like to compare against 400: 如果要与400比较,请将常数更改为400:
const ushort _WIN32_WINNT_NT4 = 400;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.