Getting character code from emoji

Just starting out? Need help? Post your questions and find answers here.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Getting character code from emoji

Post 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)
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Getting character code from emoji

Post 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)
Little John
Addict
Addict
Posts: 4770
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Getting character code from emoji

Post by Little John »

firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Getting character code from emoji

Post by firace »

Thanks, wasn't aware of this!
Post Reply