如何将Unicode字符转换为转义码

时间:2021-10-01 06:08:08

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?

所以,我有一堆像这样的字符串:{\ b \ cf12よろてそ}。我想我可以迭代每个字符并用unicode转义码(\ u ###)替换任何unicode(编辑:任何AscW(char)> 127或<0)。但是,我不确定如何以编程方式这样做。有什么建议?

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.

我有一个像{\ b \ cf12よろてそ}这样的字符串,我想要一个像{\ b \ cf12 [STUFF]}这样的字符串,当我查看rtf文本时,[STUFF]将显示为よろてそ。

2 个解决方案

#1


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

您只需使用AscW()函数即可获得正确的值: -

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.

注意,与unicode的其他转义不同,RTF使用十进制签名的短int(2字节)表示形式的unicode字符。这使得VB6中的转换非常容易。

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.

正如MarkJ在评论中指出的那样,你只会对0-127之外的字符进行此操作,但是你也需要在0-127范围内特殊处理中给出一些其他字符。

#2


Another more roundabout way, would be to add the MSScript.OCX to the project and interface with VBScript's Escape function. For example

另一种更迂回的方式是将MSScript.OCX添加到项目中并与VBScript的Escape函数接口。例如

Sub main()
    Dim s As String
    s = ChrW$(&H3088) & ChrW$(&H308D) & ChrW$(&H3066) & ChrW$(&H305D)
    Debug.Print MyEscape(s)
End Sub

Function MyEscape(s As String) As String
    Dim scr As Object
    Set scr = CreateObject("MSScriptControl.ScriptControl")
    scr.Language = "VBScript"
    scr.Reset
    MyEscape = scr.eval("escape(" & dq(s) & ")")
End Function

Function dq(s)
    dq = Chr$(34) & s & Chr$(34)
End Function

The Main routine passes in the original Japanese characters and the debug output says:

Main例程传入原始日文字符,调试输出显示:

%u3088%u308D%u3066%u305D

HTH

#1


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

您只需使用AscW()函数即可获得正确的值: -

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.

注意,与unicode的其他转义不同,RTF使用十进制签名的短int(2字节)表示形式的unicode字符。这使得VB6中的转换非常容易。

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.

正如MarkJ在评论中指出的那样,你只会对0-127之外的字符进行此操作,但是你也需要在0-127范围内特殊处理中给出一些其他字符。

#2


Another more roundabout way, would be to add the MSScript.OCX to the project and interface with VBScript's Escape function. For example

另一种更迂回的方式是将MSScript.OCX添加到项目中并与VBScript的Escape函数接口。例如

Sub main()
    Dim s As String
    s = ChrW$(&H3088) & ChrW$(&H308D) & ChrW$(&H3066) & ChrW$(&H305D)
    Debug.Print MyEscape(s)
End Sub

Function MyEscape(s As String) As String
    Dim scr As Object
    Set scr = CreateObject("MSScriptControl.ScriptControl")
    scr.Language = "VBScript"
    scr.Reset
    MyEscape = scr.eval("escape(" & dq(s) & ")")
End Function

Function dq(s)
    dq = Chr$(34) & s & Chr$(34)
End Function

The Main routine passes in the original Japanese characters and the debug output says:

Main例程传入原始日文字符,调试输出显示:

%u3088%u308D%u3066%u305D

HTH