如何在nodejs(电子)中正确显示变音符?



我有以下函数在我的c#库返回一个xml字符串

[DllExport]
public static string ReadData(ushort port){
...
return xmlString;
}

我有一个包含字母"ö"在Nodejs (Electron)中显示为´┐¢。当在我的html文件中包含值并显示它时,只有一个问号,我不太确定为什么。xml字符串listUTF-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/Encoder,但似乎没有帮助。

编辑:我找到了一个解决方法。我编写了一个函数,它将变音符转换为特定的字符,以便我可以在node.

中对它们进行反向转换。

要确保字符串具有正确的编码,可以将字符串强制转换为Buffer,然后将其强制转换回字符串,确保它具有UTF-8编码,如下所示:

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

最新更新