0

I am trying to send a string containing non-ASCII characters from node to a C++ native module but I seem to be getting garbage instead of the special characters (regular ASCII ones are ok).

In JS I just call my function with this hardcoded string "/ÿaddoné/" but on the C++ side I get "/�addon�/".

I use the following to print the string

std::cout << std::string(*v8::String::Utf8Value(obj->Get(v8::String::NewFromUtf8(isolate, "data")))) << std::endl;

What am I doing wrong ? How can I fix this?

user1233963
  • 1,450
  • 15
  • 41
  • 1
    Are you sure your console can display the characters? – NathanOliver May 21 '18 at 14:31
  • 1
    Seems like related to https://stackoverflow.com/questions/45575863/how-to-print-utf-8-strings-to-stdcout-on-windows – Öö Tiib May 21 '18 at 14:36
  • the console can display the characters, if I do cout << "/ÿaddoné/" it prints fine – user1233963 May 21 '18 at 14:48
  • @user1233963 (with regards to that cout test): are you sure your source file is encoded as UTF-8? –  May 21 '18 at 14:55
  • @Frank no, the cpp source file was ISO 8859-1, I converted to UTF-8 and it now it doesn't work. What's the idea here? – user1233963 May 21 '18 at 14:59
  • @user1233963 See ÖöTiib's comment, it's exactly that. In short, `cout` doesn't interpret utf-8, it just passes the bytes to the console as-is, which interprets them based on its configuration (in Windows: ISO 8859-1) –  May 21 '18 at 15:01
  • @Frank I don't understand. What's the encoding of the cpp file have to do with this ? I send the string from js (source is also 8859-1) how is that related ? – user1233963 May 21 '18 at 15:12
  • @user1233963 the fact that it printed correctly when the file was encoded as ISO 8859-1 proves that your console is interpreting the string as that encoding. Your code forces a conversion to UTF-8, which is going to be a mismatch. –  May 21 '18 at 15:15
  • @Frank how should I get the value without forcing the conversion? – user1233963 May 22 '18 at 18:55

0 Answers0