转换的十六进制格式

时间:2014-09-02 17:29:48

标签: hex

我有一个十六进制值,格式为:

0x00000000:0x00000000:0x00000000:0x00000000

我需要将其转换为如下格式:

00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00

我希望能够剥离0x'并且每两个字符插入一个冒号,但这似乎不正确。如何转换这两种格式?

1 个答案:

答案 0 :(得分:0)

您可以使用字符串完全执行此操作。在C#中:

    string original = "0x00000000:0x00000000:0x00000000:0x00000000";

    //remove the unwanted characters
    string[] split = { "0x", ":" };
    split = original.Split(split, StringSplitOptions.RemoveEmptyEntries);

    //format the rest in 2-char chunks
    string result = "";
    for (int i = 0; i < split.Length; i++)
    {
        for (int j = 0; j < split[i].Length; j += 2)
        {
            result += split[i].Substring(j, 2) + ":";
        }
    }

    //remove the trailing ':'
    result = result.Substring(0, result.Length - 1);

为清晰起见,省略了错误处理。

这有用吗?