How to insert a Symbol (Pound, Euro, Copyright) into a Textbox - c#

I can use the Alt Key with the Number Pad to type symbols, but how do I programmatically insert a Symbol (Pound, Euro, Copyright) into a Textbox?
I have a configuration screen so I need to dynamically create the \uXXXX's.

In C#, the Unicode character literal \uXXXX where the X's are hex characters will let you specify Unicode characters. For example:
\u00A3 is the Pound sign, £.
\u20AC is the Euro sign, €.
\u00A9 is the copyright symbol, ©.
You can use these Unicode character literals just like any other character in a string.
For example, "15 \u00A3 per item" would be the string "15 £ per item".
You can put such a string in a textbox just like you would with any other string.
Note: You can also just copy (Ctrl+C) a symbol off of a website, like Wikipedia (Pound sign), and then paste (Ctrl+V) it directly into a string literal in your C# source code file. C# source code files use Unicode natively. This approach completely relieves you from ever even having to know the four hex digits for the symbol you want.
To parallel the example above, you could make the same string literal as simply "15 £ per item".
Edit: If you want to dynamically create the Unicode character from its hex string, you can use this:
public static char HexToChar(string hex)
{
return (char)ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}
For example, HexToChar("20AC") will get you the Euro sign.
If you want to do the opposite operation dynamically:
public static string CharToHex(char c)
{
return ((ushort)c).ToString("X4");
}
For example CharToHex('€') will get you "20AC".
The choice of ushort corresponds to the range of possible char values, shown here.

I cant believe this was difficult to find on the internet!
For future developers,if you have the unicode character its easy to do. eg:
C#:
var selectionIndex = txt.SelectionStart;
string copyrightUnicode = "00A9";
int value = int.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber);
string symbol = char.ConvertFromUtf32(value).ToString();
txt.Text = txt.Text.Insert(selectionIndex, symbol);
txt.SelectionStart = selectionIndex + symbol.Length;
VB.Net
Dim selectionIndex = txt.SelectionStart
Dim copyrightUnicode As String = "00A9"
Dim value As Integer = Integer.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber)
Dim symbol As String = Char.ConvertFromUtf32(value).ToString()
txt.Text = txt.Text.Insert(selectionIndex, symbol)
txt.SelectionStart = selectionIndex + symbol.Length

Related

How to convert unicode set from db to characters?

I need to convert unicode characters that I take from the database field to a string value. In the database field unicode characters are in format U+0024 and next I get \u0024 format but I cannot convert it.
string a = "U+0024";
string b = a.Remove(0, 2);
string c = #"\u" + b;
string d = System.Uri.UnescapeDataString(c);
Console.WriteLine(d);
// There is \u0024 in output
string e =System.Uri.UnescapeDataString(\u0024);
Console.WriteLine(e);
//There is $ in output that I would like to
The strings you got from your DB seems to be Unicode codepoints, as they are in the format U+XXXX.
There is a very useful method called char.ConvertFromUtf32 that converts a Unicode code point to a string containing a single char, or a surrogate pair of chars.
This method accepts an int as parameter, so you would need to convert your b string (which is in hexadecimal) into an int.
int codepoint = Convert.ToInt32(b, 16);
Then, pass it to ConvertFromUtf32:
string result = char.ConvertFromUtf32(codepoint);

C# - Is there no simple way to concatenate hex and ASCII into a single string

In Python I'm able to concatenate both hex and ascii into a single string, then send it to the device I'm trying to control
_startTransmit = "\x02"
_channel = 7
command_string = _startTransmit + "PGM:" + str(_channel) + ";"
try:
written = conn.write(command_string.encode('utf-8'))
This SO Answer makes it appear building a byte array is the only way to do it in C#
So, it appears there is no similar way to concatenate hex and ascii in C# as there is in Python. Am I correct?
I'd look at using the string interpolator operator $ before the "string":
var _startTransmit = (char)0x02; //ints can be written in 0xFF format in the source code and cast to char e.g. (char)0x41 is a capital A
var _channel = 7;
var command_string = $"{_startTransmit}PGM:{_channel};"
The last line is syntactic sugar for:
var command_string = string.Format("{0}PGM:{1};", _startTransmit, _channel);
The things in {brackets} can have format specifiers, eg pad out the _channel to 4 chars with leading spaces, zeroes etc {_channel:0000} - see https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8

Use a Decimal Value as a Hexadecimal Value

I have the int 15 (or the string "15", that's just as easy), and I need to use it to create the value:
"\u0015"
Is there some conversion which would accomplish this? I can't do this:
"\u00" + myInt.ToString()
Because the first literal is invalid. Is there a simple way to get this result?
(If you're curious, this is for integrating with a hardware device where the vendor sometimes expresses integer values as hexadecimal. For example, I'd need to send today's date to the device as "\u0015\u0010\u0002".)
Given that you want a Unicode code point of 21, not 15, you should definitely start with the string "15". If you try to start with 15 as an int, you'll find you can't express anything with a hex representation involving A-F...
So, given "15" the simplest way of parsing that as hex is probably:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
After that, you just need to cast to char:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
char character = (char) codePoint;
Note that this will only work for code points in the Basic Multilingual Plane (BMP) - i.e. U+0000 to U+FFFF. If you need to handle values beyond that (e.g. U+1F601) then you should use char.ConvertFromUtf32 instead:
string text = "15";
int codePoint = Convert.ToInt32(text, 16);
string character = char.ConvertFromUtf32(codePoint);
Unicode literals in strings are resolved at compile-time, that's why "\u00" + myInt.ToString() doesn't work (the ToString() and concatenation are evaluated at runtime).
You could cast the int to char:
int unicodeCodePoint = 15; // or 21
char c = (char)unicodeCodePoint;

Six digit unicode escaped value comparison

I have a six digit unicode character, for example U+100000 which I wish to make a comparison with a another char in my C# code.
My reading of the MSDN documentation is that this character cannot be represented by a char, and must instead be represented by a string.
a Unicode character in the range U+10000 to U+10FFFF is not permitted in a character literal and is represented using a Unicode surrogate pair in a string literal
I feel that I'm missing something obvious, but how can you get the follow comparison to work correctly:
public bool IsCharLessThan(char myChar, string upperBound)
{
return myChar < upperBound; // will not compile as a char is not comparable to a string
}
Assert.IsTrue(AnExample('\u0066', "\u100000"));
Assert.IsFalse(AnExample("\u100000", "\u100000")); // again won't compile as this is a string and not a char
edit
k, I think I need two methods, one to accept chars and another to accept 'big chars' i.e. strings. So:
public bool IsCharLessThan(char myChar, string upperBound)
{
return true; // every char is less than a BigChar
}
public bool IsCharLessThan(string myBigChar, string upperBound)
{
return string.Compare(myBigChar, upperBound) < 0;
}
Assert.IsTrue(AnExample('\u0066', "\u100000));
Assert.IsFalse(AnExample("\u100022", "\u100000"));
To construct a string with the Unicode code point U+10FFFF using a string literal, you need to work out the surrogate pair involved.
In this case, you need:
string bigCharacter = "\uDBFF\uDFFF";
Or you can use char.ConvertFromUtf32:
string bigCharacter = char.ConvertFromUtf32(0x10FFFF);
It's not clear what you want your method to achieve, but if you need it to work with characters not in the BMP, you'll need to make it accept int instead of char, or a string.
As per the documentation for string, if you want to iterate over characters in a string as full Unicode values, use TextElementEnumerator or StringInfo.
Note that you do need to do this explicitly. If you just use ordinal values, it will check UTF-16 code units, not the UTF-32 code points. For example:
string text = "\uF000";
string upperBound = "\uDBFF\uDFFF";
Console.WriteLine(string.Compare(text, upperBound, StringComparison.Ordinal));
This prints out a value greater than zero, suggesting that text is greater than upperBound here. Instead, you should use char.ConvertToUtf32:
string text = "\uF000";
string upperBound = "\uDBFF\uDFFF";
int textUtf32 = char.ConvertToUtf32(text, 0);
int upperBoundUtf32 = char.ConvertToUtf32(upperBound, 0);
Console.WriteLine(textUtf32 < upperBoundUtf32); // True
So that's probably what you need to do in your method. You might want to use StringInfo.LengthInTextElements to check that the strings really are single UTF-32 code points first.
From https://msdn.microsoft.com/library/aa664669.aspx, you have to use \U with full 8 hex digits. So for example:
string str1 = "\U0001F300";
string str2 = "\uD83C\uDF00";
bool eq = str1 == str2;
using the :cyclone: emoji.

C# change numbers to small indexes in string

I have string "H20" (chemical formula for water). I would like to change it so that all the numbers in the string would be small (the number 2 would be small index next to the letter H). How can I do that?
Assuming that you have the means of displaying the subscript unicode characters, you could easily write your own extension method for subscripting:
public static string Subscript(this string normal)
{
if(normal == null) return normal;
var res = new StringBuilder();
foreach(var c in normal)
{
char c1 = c;
// I'm not quite sure if char.IsDigit(c) returns true for, for example, '³',
// so I'm using the safe approach here
if (c >= '0' && c <= '9')
{
// 0x208x is the unicode offset of the subscripted number characters
c1 = (char)(c - '0' + 0x2080);
}
res.Append(c1);
}
return res.ToString();
}
As has been pointed out in the comments, you should typically use some presentation technology for this kind of formatting. For example, in HTML, you could display your text through:
<span>H<sub>2</sub>O</span>
However, Unicode allocates a superscripts and subscripts block for hexadecimal characters which you can take advantage of. Since Unicode is supported natively in .NET, including within string literals, you can use your desired character directly:
text = text.Replace("H2O", "H₂O");
Note: Using the Unicode subscript character would guarantee that your H₂O string would render correctly in any Unicode-aware application, irrespective of its formatting technology (HTML, RTF, PDF, XPS, etc).
Below is a screenshot showing how the string renders in a TextBox under Windows Forms. To improve legibility, font has been changed to Cambria, 11.25pt.
Edit: If you want to convert all numerals (not just 2 into ₂), you could use #Tobias’s code. Here is a regex adaptation of it. I’ve included a lookbehind since I assume that all numerals to be subscripted must be preceded by a letter.
text = Regex.Replace(text, #"(?<=[A-Za-z])\d",
match => ((char)(match.Value[0] - '0' + '₀')).ToString());
The above would transform a string like
CF3CH2Cl + Br2 → CF3CHBrCl + HBr
into
CF₃CH₂Cl + Br₂ → CF₃CHBrCl + HBr

Categories

Resources