4

I'm using font awesome in a Xamarin application. The api to which the XamarinApp is talking returns a fxxx string to indicate which icon to show. In code I add \u but it is seen as a string and not a unicode character.

    var value = "f641";
    newLabel.Text = char.Parse($"\\u{value}").ToString();

I tried char.Parse but it throws an error:

System.FormatException: String must be exactly one character long.

Any suggestions ?

BrilBroeder
  • 1,409
  • 3
  • 18
  • 37
  • .NET strings *are* Unicode. Insterting `\ ` and `u` in a string just adds two more characters. I suspect you confused Unicode with Unicode escape sequences in string *literals*? Those sequences represent *single* characters. During compilation, the compiler itself will convert them to the actual character. `f461` isn't a valid Unicode escape sequence anyway. What is `f641` supposed to be? – Panagiotis Kanavos May 07 '19 at 08:40
  • the idea is to prepend f641 when delivered from the api with \u and parse that new string \uf641 to a char. In fontawasome it is some icon. https://fontawesome.com/cheatsheet – BrilBroeder May 07 '19 at 08:46
  • You're confusing character escape sequences for some kind of special text. An escape sequence represents a *single* character. You could just copy that character from the list you posted instead of using its hex value. The compiler will replace such sequences with the corresponding characters before it even starts compilation. Fonts don't translate anything, they contain glyphs that are used to display characters. What you typed is probably the hex value of a Unicode code point, ie `0xF641`. – Panagiotis Kanavos May 07 '19 at 08:56
  • 1
    In this case, you could store the actual character instead of its hex value as a string. Why store `2665` as a string for example and not just type `♥` ? Or just store the *integer* value somewhere? If you can't do that (*Why ?*), you could parse the hex value and then use [Char.ConvertFromUtf32](https://learn.microsoft.com/en-us/dotnet/api/system.char.convertfromutf32?view=netframework-4.8) to convert it into a character. – Panagiotis Kanavos May 07 '19 at 08:58
  • @Panagiotis _"The api to which the XamarinApp is talking returns a fxxx string"_ – CodeCaster May 07 '19 at 09:10
  • @CodeCaster which means the API returns a hex value that could be treated as a number. I suspect Json.NET could use a custom converter that treated hex values as integers – Panagiotis Kanavos May 07 '19 at 09:13
  • 1
    @Pana good point. Or simply add a getter that does that for you. – CodeCaster May 07 '19 at 09:16
  • @BrilBroeder does the API return JSON, XML or something else? Which library do you use do deserialize the response? Perhaps you could add a custom type converter for that specific property that converts the hex directly into a Char – Panagiotis Kanavos May 07 '19 at 09:17
  • I just created a string extension method that takes the "hex-string" and returns the converted string. – BrilBroeder May 07 '19 at 09:29
  • 1
    Possible duplicate of [converting unicode to actual character C#](https://stackoverflow.com/q/36611182/1016716), [How to decode a Unicode character in a string](https://stackoverflow.com/q/9303257/1016716), [Unicode characters string](https://stackoverflow.com/q/11700800/1016716), [C# Convert Unicode chars](https://stackoverflow.com/q/39620196/1016716), [C# Convert string to char - Unicode](https://stackoverflow.com/q/44522235/1016716), [how to convert string of unicodes to the char?](https://stackoverflow.com/q/5865718/1016716) etc. – Mr Lister May 07 '19 at 09:53

1 Answers1

6

You want to have a Char holding the value of the private-use code point U+F641.

You can do so by parsing it as the hexadecimal value it represents:

var input = "f641";
int p = int.Parse(input, System.Globalization.NumberStyles.HexNumber); // 63041

And then convert it to a Char:

char c = (char)p;

Depending on the range of possible code points, you may not have enough space in a char to store the code point, so as @Panagiotis indicates, use Char.ConvertFromUtf32(int):

string chars = Char.ConvertFromUtf32(p);

But then you'll have a string, not a single char.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272