如何在nodejs(电子)中正确显示umlauts?

发布于 2025-01-23 13:31:38 字数 733 浏览 4 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2025-01-30 13:31:38

为了确保您的字符串具有正确的编码,您可以将字符串施放到缓冲区中,然后将其施放回一个字符串,以确保其具有UTF-8编码,例如:

console.log(util.inspect(Buffer.from(readDataLibrary.ReadData(8192), 'utf-8').toString(), true, null, true))

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:

console.log(util.inspect(Buffer.from(readDataLibrary.ReadData(8192), 'utf-8').toString(), true, null, true))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文