How to convert Unicode characters to escape codes

rtf, unicode, utf-8, vb6

Solution

You can simply use the AscW() function to get the correct value:-

sRTF = "\u" & CStr(AscW(char))

Note unlike other escapes for unicode, RTF uses the decimal signed short int (2 bytes) representation for a unicode character. Which makes the conversion in VB6 really quite easy.

Edit

As MarkJ points out in a comment you would only do this for characters outside of 0-127 but then you would also need to give some other characters inside the 0-127 range special handling as well.

Problem

So, I have a bunch of strings like this: {\b\cf12 よろてそ } . I'm thinking I could iterate over each character and replace any unicode (Edit: Anything where `AscW(char) > 127 or < 0`) with a unicode escape code (\u###). However, I'm not sure how to programmatically do so. Any suggestions? Clarification: I have a string like {\b\cf12 よろてそ } and I want a string like {\b\cf12 [STUFF]}, where [STUFF] will display as よろてそ when I view the rtf text.

Original source