The perl code is basically converting a unicode value to UTF-16 and formatting it as a rtf sequence.
I have supplied some Xojo code below which should do the same as well as handle ASCII & Unicode Plane 0 characters. It doesn’t handle tabs / carriage returns though.
It looks like RTF can accept the \u sequences either as signed or unsigned 16 bit values.
so… \u-497 in your example is the same as \u65039 (-497 + 65536)
The character after the \u (? or '5f) is to tell the RTF parser what character to display if it doesn’t understand the \u sequence.
The number of characters you have to specify is based on the \uc command which defaults to 1 (\uc0 means don’t display anything).
Const kEndOfASCII = &h7F
Const kMaxSigned16BitValue = &h7FFF
Const kStartOfUnicodePlane1 = &h10000
Const kUTF16HighSurrogateStart = &hD800
Const kUTF16LowSurrogateStart = &hDC00
Const kHigh10Bits = &hFFC00
Const kHigh10BitsShiftToLow10Bits = &h400
Const kLow10Bits = &h3FF
Const kUnsigned16BitValueIntoSigned16BitValueRange = &h10000
Dim r As String
Dim s As String
Dim u As UInt32
Dim w1, w2 As Int16
s = "😀"
r = ""
u = Asc(s)
'note. the following is based on \uc being 1
If u <= kEndOfASCII Then
'ascii
r = s
ElseIf u <= kMaxSigned16BitValue Then
'unicode plane 0 (values 0 - 32767)
r = "\u" + Str(u) + "\'5f"
ElseIf u < kStartOfUnicodePlane1 Then
'unicode plane 0 (values 32768 - 65535)
r = "\u" + Str(u - kUnsigned16BitValueIntoSigned16BitValueRange) + "\'5f"
Else
'unicode plane 1 upwards
'convert unicode plane 1 to utf16
u = u - kStartOfUnicodePlane1
w1 = kUTF16HighSurrogateStart + ((u And kHigh10Bits) / kHigh10BitsShiftToLow10Bits) - kUnsigned16BitValueIntoSigned16BitValueRange
w2 = kUTF16LowSurrogateStart + (u And kLow10Bits) - kUnsigned16BitValueIntoSigned16BitValueRange
'add the high & low surrogates
r = "\u" + Str(w1) + "\'5f" + "\u" + Str(w2) + "\'5f"
End If
Break