views:

447

answers:

2

I have a string that I want to display a symbol in (the division symbol you learn in elementary, not a slash). According to the character map, the font that I'm using to display the string (inkpen2) has a division symbol code of 0xD4. I want a string to be "5 symbol 7" and display it to the user. Is it possible to do this?

+6  A: 
string oneWay = "5 ÷ 7";
string anotherWay = "5 \u00F7 7";

Note that the first way can break if your source code ever has high bits stripped or is read by an app using some old text encodings. The second way is safer, just harder to read.

If you are using typographic math symbols, you may also want to use \u00D7 or × instead of *.

I don’t know where you get D4 from, but the Unicode value for the division symbol is F7, and C# uses unicode for all fonts.

Dour High Arch
0xD4 is in a font called Inkpen2.
SLaks
You could make it more readable by binding the unicode of the division symbol to a constant like `const string DivisionSymbol = "\u00F7"` and then doing something like `string anotherWay = string.Format("5 {0} 7", DivisionSymbol);`
cdmckay
You could make it even more readable by embedding the Unicode character. I don't know of any reason not to.
SLaks
@SLaks: Right, because everyone knows off the top of their head that `\u00F7` means `÷`.
cdmckay
I think SLaks means to use ÷ instead of \u00F7. I added why that may be a bad idea.
Dour High Arch
@Dour High Arch: Yes, I did.
SLaks
@SLaks: My mistake.
cdmckay
+1  A: 

The beast way to do this is to use the standard Unicode code point instead of a custom font. In your case, you want U+00f7 ÷.

You can put that character in a string within your source code the same way you would use any other character, and it should work on almost any computer.


If you want to use a custom font, you can use the WinForms RichTextBox Control to display text with multiple fonts. If your entire string is in the same font, you can use a regular Label control and set its Font property. Note that you will need to distribute the font to your end-users, and that most fonts have license restrictions.

SLaks