Page 1 of 1

Getting character code from emoji

Posted: Mon Mar 31, 2025 8:34 pm
by firace
Elephant emoji is character 128024. (I found this by googling for it.)
How can I find this number using PureBasic?

In other words I'm looking for the reverse function of STARGÅTE's UnicodeChr procedure.

I asked ChatGPT but it seems it can't get the correct result!

Code: Select all


Procedure.s UnicodeChr(Number.i)  ; procedure by STARGÅTE 
  Protected Buffer.l
  If Number <= $FFFF
    ProcedureReturn Chr(Number)
  Else
    Buffer = (Number&$3FF)<<16 | (Number-$10000)>>10 | $DC00D800
    ProcedureReturn PeekS(@Buffer, 2, #PB_Unicode)
  EndIf
EndProcedure


Debug "🐘"
Debug UnicodeChr(128024)

Re: Getting character code from emoji

Posted: Mon Mar 31, 2025 8:57 pm
by firace
Solved! After asking ChatGPT again (and fixing a few things) :)

Code: Select all

Define emoji.s = "🐘"  ; Elephant emoji 
Define highSurrogate, lowSurrogate, unicodeValue

; Extract UTF-16 surrogate pair
highSurrogate = Asc(emoji)   ; First character (could be high surrogate)
lowSurrogate  = Asc(Mid(emoji, 2, 1))  ; Second character (if surrogate pair)

; If high surrogate is in the valid range (U+D800 to U+DBFF)
If highSurrogate >= $D800 And highSurrogate <= $DBFF
  unicodeValue = ((highSurrogate - $D800) << 10) | (lowSurrogate - $DC00) + $10000
Else
  unicodeValue = highSurrogate  ; Single-character BMP Unicode
EndIf

Debug "Unicode Code Point: " + Str(unicodeValue)

Re: Getting character code from emoji

Posted: Mon Mar 31, 2025 9:12 pm
by Little John

Re: Getting character code from emoji

Posted: Mon Mar 31, 2025 9:44 pm
by firace
Thanks, wasn't aware of this!