1

I'm trying to convert an emoji to an hex number or a string.

there is any way to convert this in this : 0x00000000D83DDC71L or D83DDC71

Edit

my code is this:

var bytes = Encoding.UTF8.GetBytes(emoji.ToString()); //emoji is 
var number = BitConverter.ToUInt32(bytes, 0); //number is 2610470896
var emojiCode = unicode.ToString("X"); // emojiCode is 9B989FF0

the problem is that i need my emojiCode to be D83DDC71

i hope is more clear now.

frenk91
  • 919
  • 1
  • 15
  • 30
  • possible duplicate of [convert string to hex-string in C#](http://stackoverflow.com/questions/16999604/convert-string-to-hex-string-in-c-sharp) – MakePeaceGreatAgain Jul 09 '15 at 12:33
  • Could you be more specific? There's obvious solutions like converting the filename or hash, but I suppose you thought about that already? – runDOSrun Jul 09 '15 at 12:33

2 Answers2

2

You have to do something like:

var str = "\uD83D\uDC71";
string res = BitConverter.ToString(Encoding.BigEndianUnicode.GetBytes(str)).Replace("-", "");

Note that you want your Unicode string to be in "big endian" mode (so Encoding.BigEndianUnicode)

Probably easier without going through the Encoding conversion:

string res = string.Concat(str.Select(x => ((ushort)x).ToString("X4")));

(ushort and char are nearly the same thing, but ushort is built to be formatted as a number, while char is built to be formatted as a character)

xanatos
  • 109,618
  • 12
  • 197
  • 280
0

Emoji Unicode is not a single hex number, and it only encoding by UTF32. So you could split it, like this:

byte[] utfBytes = System.Text.Encoding.UTF32.GetBytes("");
print(utfBytes.Length);
for (int i = 0; i < utfBytes.Length; i += 4)
{
     if (i != 0) result += '-';
     result += System.BitConverter.ToInt32(utfBytes, i).ToString("x2").ToUpper();
}
Li Yuan
  • 1
  • 1
  • 1
    Welcome to StackOverflow. Please do not post code without explanation. Please share with us your thoughts why do you think your proposed solution might help the OP. – Peter Csala Sep 23 '20 at 06:49