将自定义数字格式字符串与十六进制格式字符串相结合

发布于 2024-08-17 21:46:59 字数 1011 浏览 6 评论 0原文

我遇到一种情况,我需要根据整数是否为零来不同地格式化它。使用自定义数字格式字符串,可以通过分号作为分隔符来实现。请考虑以下事项:

// this works fine but the output is decimal
string format = "{0:0000;-0000;''}";
Console.WriteLine(format,  10); // outputs "0010"
Console.WriteLine(format, -10); // outputs "-0010"
Console.WriteLine(format,   0); // outputs ""

但是,我想使用的格式是十六进制。我希望输出更像是:

// this doesn't work
string format = "{0:'0x'X8;'0x'X8;''}";
Console.WriteLine(format,  10); // desired "0x0000000A", actual "0xX8"
Console.WriteLine(format, -10); // desired "0xFFFFFFF6", actual "0xX8"
Console.WriteLine(format,   0); // desired "", actual ""

不幸的是,当使用自定义数字格式字符串时,我不确定如何(如果可能的话)使用自定义格式字符串中数字的十六进制表示形式。我所遇到的场景不允许有太大的灵活性,因此采用两遍格式不是一种选择。无论我做什么都需要表示为 String.Format 样式格式字符串。

编辑
在查看了 NumberFormatter 的 Mono 源代码(.NET 实现仅遵循内部非托管代码)后,我证实了我的怀疑。十六进制格式字符串被视为特殊情况,它只能用作标准格式字符串,不能在自定义格式字符串中使用。由于三部分格式字符串不能与标准格式字符串一起使用,所以我非常 SOL,

我可能会硬着头皮将整数属性设置为可为 null 的 int,并在我使用零的地方使用 null。

I have a situation where I need to format an integer differently depending on whether or not it's zero. Using custom numeric format strings, this can be achieved with the semicolon as a separator. Consider the following:

// this works fine but the output is decimal
string format = "{0:0000;-0000;''}";
Console.WriteLine(format,  10); // outputs "0010"
Console.WriteLine(format, -10); // outputs "-0010"
Console.WriteLine(format,   0); // outputs ""

However, the format that I want to use is hex. What I would like the output to be is more like:

// this doesn't work
string format = "{0:'0x'X8;'0x'X8;''}";
Console.WriteLine(format,  10); // desired "0x0000000A", actual "0xX8"
Console.WriteLine(format, -10); // desired "0xFFFFFFF6", actual "0xX8"
Console.WriteLine(format,   0); // desired "", actual ""

Unfortunately, when using a custom numeric format string, I am not sure how (if it's even possible) to use the hex representation of the number in the custom format string. The scenario I have doesn't permit much flexibility so doing a two-pass format isn't an option. Whatever I do needs to be represented as a String.Format style format string.

EDIT
After looking over the Mono source for NumberFormatter (the .NET implementation simply defers to internal unmanaged code) I've confirmed my suspicions. The hex format string is treated as a special case and it is only available as a standard format string and cannot be used in a custom format string. And since a three part format string can't be used with a standard format string, I'm pretty much S.O.L.

I will probably just bite the bullet and make the integer property a nullable int and use nulls where I was using zero - bleh.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-08-24 21:46:59

以下格式字符串几乎是正确的:

string format = "0x{0:X8}";
Console.WriteLine(format, 10);
Console.WriteLine(format, -10);
Console.WriteLine(format, 0);

给出:

0x0000000A

0xFFFFFFF6

0x00000000

我仍在尝试将 '' 处理为 0。

编辑:我遇到了同样的问题,只有当格式字符串具有分隔符 ';' 时,X8 才会变成文字在使用中。我只是要浏览一下 .NET 源代码并看看我能看到什么。

编辑2:以下扩展方法将返回一个格式正确的字符串,包括+'ves、-'ves和0。参数长度是十六进制字符串中所需的字符数(不包括前面的'0x') )。

    public static string ToHexString(this int source, int length)
    {
        return (source != 0) ? string.Format("0x{0:X" + length.ToString() + "}",source) : string.Empty;
    }

The following format string is ALMOST correct:

string format = "0x{0:X8}";
Console.WriteLine(format, 10);
Console.WriteLine(format, -10);
Console.WriteLine(format, 0);

gives:

0x0000000A

0xFFFFFFF6

0x00000000

I'm still trying to work the '' for 0.

EDIT: I am having the same issue, with the X8's becoming literals only when the format string has the seperator ';' in use. I'm just going to poke around in the .NET source and see what I can see.

EDIT 2: The follow extension method will return a string with the correct formatting, both for +'ves, -'ves and 0. The parameter length is the amount of characters required in the hex string (excluding the '0x' on the front).

    public static string ToHexString(this int source, int length)
    {
        return (source != 0) ? string.Format("0x{0:X" + length.ToString() + "}",source) : string.Empty;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文