最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node encode and decode utf-16 buffer - Stack Overflow

programmeradmin2浏览0评论

im working on a javascript/nodejs application that needs to talk with a C++ tcp/udp socket. It seems like I get from the old C++ clients an utf16 buffer. I didn't found a solution right now to convert it to a readable string and the other direction seems to be the same problem.

Is there a easy way for this two directions?

Nice greetings

im working on a javascript/nodejs application that needs to talk with a C++ tcp/udp socket. It seems like I get from the old C++ clients an utf16 buffer. I didn't found a solution right now to convert it to a readable string and the other direction seems to be the same problem.

Is there a easy way for this two directions?

Nice greetings

Share Improve this question asked Nov 4, 2016 at 8:44 user2244925user2244925 2,3543 gold badges15 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 15

If you have a UTF-16-encoded buffer, you can convert it to a UTF-8 Javascript string like this:

let string = buffer.toString('utf16le');

To read these from a stream, it's easiest to use convert to string at the very end:

let chunks = [];
stream.on('data', chunk => chunks.push(chunk))
      .on('end',  ()    => {
        let buffer = Buffer.concat(chunks);
        let string = buffer.toString('utf16le');
        ...
      });

To convert a JS string to UTF-16:

let buffer = Buffer.from(string, 'utf16le')
发布评论

评论列表(0)

  1. 暂无评论