连接字符串,转换为ushort,与ushort进行比较

时间:2017-10-19 01:00:53

标签: c# string uint16 ushort

所以我有一些常数:

const ushort _WIN32_WINNT_NT4 = 0x0400;
const ushort _WIN32_WINNT_WIN2K = 0x0500;
....

然后我有一个主要版本号,次要版本号和服务包号,当你将它们连接在一起时,它与上面的数字相同 - 除了其中2个是{{1}一个是int。我可以将它们全部变成这样的字符串:

string

对于Windows 2000,这看起来像string version = majorVersion.ToString() + minorVersion.ToString() + sp; 。它"匹配" ushort,没有"500"

我想做的是将0x0移交给函数,作为返回正确操作系统的version

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.";
    }
 }

并说它将其作为ushort uniNum = Convert.ToUInt16(version); 发送,常量为500,因此它永远不会找到操作系统并返回0x0500。当我调试并将鼠标悬停在Unknown OS version上时,它实际上是_WIN32_WINNT_WIN2K的十进制格式。  1280显示为_WIN32_WINNT_NT4,因此1024永远不会与之匹配。

如果我包含" 0x0":

"400"

它给出了输入格式不正确的错误。

我可能遗漏了一些简单的东西,但我找不到任何有用的东西。

2 个答案:

答案 0 :(得分:1)

您已经拥有常量,它们是十六进制的。如果您获得{ "version": 3, "terraform_version": "0.9.3", "serial": 14, "lineage": "dc16a61f-72dd-435b-ba3f-5e36e14aace2", "modules": [ { "path": [ "root" ], "outputs": {}, "resources": { "aws_autoscaling_group.main": { "type": "aws_autoscaling_group", "depends_on": [ "aws_launch_configuration.lc" ], "primary": { "id": "djin-sample-asg-stag", "attributes": { "arn": "arn:aws:autoscaling:us-east-1:174120285419:autoScalingGroup:04c470fa-45f8-4711-aa31-b3ede40d6… 400,那么它们也是十六进制的,请替换:

500

使用:

ushort uniNum = Convert.ToUInt16(version);

答案 1 :(得分:0)

您的常量声明的值是十六进制文字表达式:

@Override
public CharSequence getPageTitle(int position) {
    // This is "generic" string that we will use as the title to be replaced.
    String title = "title";

    Drawable myDrawable = oContext.getDrawable(R.drawable.alpha);

    // initiate the SpannableString builder
    SpannableStringBuilder spanBuilder = new SpannableStringBuilder(title); // space added before text for convenience

    // set the drawable's size...if it could be too big or too small for display
    myDrawable.setBounds(0, 0, 50, 50);

    // turn the Drawable into ImageSpan and align it along the baseline
    ImageSpan imageSpan = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE);

    // CRUCIAL: this is where we replace the "title" w/ the image
    // 0: we start from the beginning
    // title.length(): we are replacing the entire string
    // the last flag doesn't do anything in our case
    spanBuilder.setSpan(imageSpan, 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spanBuilder;
}

const ushort _WIN32_WINNT_NT4 = 0x0400; 相当于十六进制0x0400,十进制400。所以基本上,你不是在比较400而是在1024比较。

如果您想与400进行比较,请将常量更改为400:

1024