如何将整数参数的字符串表示形式返回为基数为16的无符号整数

时间:2012-04-30 02:48:06

标签: c# java

Java中的Integer.toHexString()的C#等价物是什么?

2 个答案:

答案 0 :(得分:2)

使用String.Format("{0:x}", x)静态方法或Int32.ToString("x")方法。

参见示例:

using System;
using System.Globalization;

class Program
{
static void Main()
{
    int x = 4067;
    string s = x.ToString("x");
    Console.WriteLine(s);      // fe3

    s = String.Format("{0:x}", x);
    Console.WriteLine(s);      // fe3

    s = String.Format("{0:X}", x);
    Console.WriteLine(s);      // FE3

    s = String.Format("{0:x6}", x);
    Console.WriteLine(s);      // 000fe3
}
}

答案 1 :(得分:0)

以下转换为十六进制

int myInt = 222;
string hexString = myInt.ToString("x")

MSDN Link