How to use string.Format() to format a hex number surrounded by curly brackets?
Asked Answered
U

4

13

Input: uint hex = 0xdeadbeef;

Required output: string result = "{deadbeef}"

First approach: Explicitly add the { and }; this works:

result = "{" + string.Format("{0:x}", hex) + "}"; // -> "{deadbeef}"

Output as decimal rather than hex using escaped curly brackets:

result = string.Format("{{{0}}}", hex); // -> "{3735928559}"

Seems promising, now all we need to do is add the :x hex specifer as per the first approach above:

result = string.Format("{{{0:x}}}", hex); // -> "{x}"

Oh dear, adding the ':x has made it output "{x}" rather than the "{deadbeef}" that I wanted.

So my question is: Must I solve this by explicitly adding the { and } as per the first example, or is there a way to do it using composite formatting and escaping the curly brackets?

Also note that this also affects string interpolation which (after all) is just converted by the compiler into a call to string.Format().

(This may well be duplicate question, but I have been unable to find a duplicate so far...)

edited

Upspring answered 13/10, 2016 at 8:42 Comment(5)
See "Escaping Braces" in msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx - basically your workaround is the solution.Lowpitched
This looks like a bug in string.Format.Petersham
@HansKesting That article is actually the answer to this question. If you post it as an answer I'll mark it as correct.Upspring
I can see that they have actually documented this as a feature, not as a bug. It is a bug though, in the sense that if you have to explain how something is broken, it doesn't make it right. But, since this is documented to behave like that you just have to work around it. If you follow the logic in the explanation of this then "{{{0}}}" shouldn't work either as 0} is not a valid parameter index, but since this works it is not consistent.Petersham
This answer addresses this issue as well ({{{0:N}}} printing {N}).Kathyrnkati
H
7

See "Escaping Braces" in http://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx - basically your workaround is a solution.

From there:

The way escaped braces are interpreted can lead to unexpected results. For example, consider the format item "{{{0:D}}}", which is intended to display an opening brace, a numeric value formatted as a decimal number, and a closing brace. However, the format item is actually interpreted in the following manner:

1.The first two opening braces ("{{") are escaped and yield one opening brace.
2. The next three characters ("{0:") are interpreted as the start of a format item.
3. The next character ("D") would be interpreted as the Decimal standard numeric format specifier, but the next two escaped braces ("}}") yield a single brace. Because the resulting string ("D}") is not a standard numeric format specifier, the resulting string is interpreted as a custom format string that means display the literal string "D}".
4. The last brace ("}") is interpreted as the end of the format item.
5. The final result that is displayed is the literal string, "{D}". The numeric value that was to be formatted is not displayed.

and as a solution, adjusted to your example:

uint hex = 0xdeadbeef;
string output = string.Format("{0}{1:x}{2}", 
                             "{", hex, "}");
Console.WriteLine(output);
Hulbard answered 13/10, 2016 at 12:8 Comment(0)
C
3

Closest i got is

string.Format("{{{0:x}\u200B}}",16)

It seems that }}} is interpreted wrong, inserting a zero-width space prevents the first two } being expanded as an escaped } character.

Collogue answered 13/10, 2016 at 8:52 Comment(0)
B
2

You can use an empty character or add brackets as arguments:

uint hex = 0xdeadbeef;
string result = string.Format("{0}{1:x}{2}", "{", hex, "}");

This will output you {deadbeef} as you wanted.

This happens because to output } in string.Format you have to escape it like this }}.

But when you enter }}} it understands this like }} } and outputs {x}. This is a design bug in C#, when you try to format your output like :x, :N or else.

You can also try

uint hex = 0xdeadbeef;
string result = string.Format("{{ {1:x} }}", hex);

But this will output you { deadbeef } with spaces.

Barbabas answered 13/10, 2016 at 8:57 Comment(3)
This is a bug in string.Format, not C#.Petersham
@LasseV.Karlsen What's the significant difference?Sugary
See my comments on the question.Petersham
R
2

Try this : Use 2 times String.Format Method like this

String result= String.Format("{{{0}}}",String.Format("{0:x}", hex));
Rumba answered 13/10, 2016 at 9:12 Comment(3)
No need to format the parameter then, you could use hex.ToString("x").Kathyrnkati
@Kathyrnkati what op is looking for wrap this hex string into curly brackets {deadbeef}Rumba
You're missing the point. Instead of String.Format("{0:x}", hex) one could simply use hex.ToString("x").Kathyrnkati

© 2022 - 2024 — McMap. All rights reserved.