1

I need to get emoticon's code in format like 1F600 or 128512. It's just a symbol in my string now.

I found that emoticons described in Uniicode encoding, but CharUnicodeInfo.GetNumericValue seems not working.

How can i do this?

Ok

var q = CharUnicodeInfo.GetNumericValue(text[0]);

where text[0] holds emoticon (in debug view it's just a picture) returns -1

Occam's chainsaw
  • 189
  • 3
  • 14

2 Answers2

3

You say

text[0] holds emoticon

This is not true.

text[0] holds the first Char. A Char is a 16 bit code unit, but emoticons don't fit in a 16 bit code unit. They are represented by two combining characters.

The first combining character is not a valid codepoint in itself, so trying to get the codepoint value returns -1; an invalid code point.

So how do you get the codepoint?

Return code point of characters in C# explains how you can get a list of all codepoints.

If you only want to get the first codepoint, you can use Char.ConvertToUtf32 directly:

int codepoint = Char.ConvertToUtf32(text, 0)
Community
  • 1
  • 1
Martijn
  • 11,964
  • 12
  • 50
  • 96
0

The reason CharUnicodeInfo.GetNumericValue returns -1 for you is because the character you're passing is not a numeric character. You need to read the documentation.

The Unicode value of an emoticon character in that block is a 32-bit value. For example, 0x1F600. So it will be a 32-bit value made up of the two 16-bit values. So if your string contains that emoticon character, the string will be length 2, and the two char values will be 0x0001 and 0xF600. Cast the char values to short and view them as hex. For example:

short c1 = (short)text[0];
short c2 = (short)text[1];

In the debugger, view c1 and c2 as hex.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351