如何在nodejs(电子)中正确显示umlauts?
我在c#-library中具有以下功能,该功能返回XML字符串
[DllExport]
public static string ReadData(ushort port){
...
return xmlString;
}
,我有一个包含字母“Ö”的标签,该标签如nodejs(electron)中的``
'┐¢
。当在我的HTML文件中加入值并显示它时,只有一个问号。我不确定为什么。 XML字符串IST UTF-8
的编码。调试C#功能时,XML字符串包含正确的字母,但在nodejs中不包含。 我使用以下代码包含DLL,以调用
const readDataLibrary = new ffi.Library(`${__dirname}/dll/MyDll`,{
"ReadData": ["string", ["ushort"]]
})
// incorrect representation of umlauts
console.log(util.inspect(readDataLibrary.ReadData(8192), true,null, true))
我在堆栈溢出上找到的不同方法的功能,例如使用textDecoder/cododer
,但似乎没有任何帮助。
编辑:我找到了解决方法。我编写了一个将umlauts转换为特定字符的函数,以便我可以将它们反转为节点。
I have following function in my C#-Library which returns a xml string
[DllExport]
public static string ReadData(ushort port){
...
return xmlString;
}
I have a tag containing the letter "ö" which is displayed like this ´┐¢
in Nodejs (Electron). When including the value in my html file and display it, there is just a question mark .I am not quite sure why. The encoding of the xml string ist UTF-8
. When debugging the C#-function the xml string contains the correct letter but not in Nodejs.
I use the following code to include the DLL in order to call the function
const readDataLibrary = new ffi.Library(`${__dirname}/dll/MyDll`,{
"ReadData": ["string", ["ushort"]]
})
// incorrect representation of umlauts
console.log(util.inspect(readDataLibrary.ReadData(8192), true,null, true))
I tried different approaches I found here on stack overflow like using the TextDecoder/Encoder
but nothing seems to help.
EDIT: I found a workaround. I wrote a function which converts umlauts to specific characters so that I can reverse convert them in node.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了确保您的字符串具有正确的编码,您可以将字符串施放到缓冲区中,然后将其施放回一个字符串,以确保其具有UTF-8编码,例如:
To ensure that your string has the correct encoding, you can cast the string into a Buffer and then cast it back to a string, ensuring it has the UTF-8 encoding, like this: