0

I am trying to wrap C++ code with NAPI (Node addon API). While writing the code, I faced the following problem.

When returning a string from napi side to js side, Non-English (Korean/Japanese/Chinese) strings are displayed as broken.

Is there any way to solve this? Any solutions or ideas would be appreciated!

The code is: (NAPI side)

#include <iostream>
#include "wrapper.h"

Napi::Value wrapper::getNameWrapper(const Napi::CallbackInfo &info)
{
    Napi::Env env = info.Env();
    std::string dcmFilePath = info[0].As<Napi::String>().Utf8Value();
    Napi::String name = Napi::String::New(env, wrapper::getName(dcmFilePath));
    return patientName;
}

std::string wrapper::getName(std::string dcmFilePath)
{
    std::stringstream returnStringStream;
    returnStringStream << "김한춘"; // Non-English string (Korean)_
    return returnStringStream.str();
}

Napi::Object wrapper::Init(Napi::Env env, Napi::Object exports)
{
    exports.Set("getName", Napi::Function::New(env, wrapper::getNameWrapper));
    return exports;
}

(js side)

const wrapper = require('./build/Release/wrapper.node')

console.log('wrapper: ', wrapper)
let name = wrapper.getName('C:/Users/p1h2c/Desktop/R2DATA/金韓春/DCT0000.dcm')
console.log('getName: ', name) // I expected '김한춘' but show '김?�춘'

module.exports = wrapper
CRESENS
  • 1
  • 1
  • *I expected '김한춘' but show 'break characters'*. Really? Please [edit] your question to provide a [mcve]. Maybe you see `김한춘` or alike? – JosefZ Sep 21 '22 at 15:55
  • Oh I'm so sorry. my console printed the follow message: 김?�춘 – CRESENS Sep 21 '22 at 16:19

0 Answers0