1

I have a project using Node.JS. I need to send a string to the terminal (UART COM-port) in Latin1. I just realised that I cannot create String object with 1-byte encoding (like Latin-1).

I need to create string from array of bytes. Bytes that a less than 128 are ASCII bytes and string with these bytes always looks good. But if I pass bytes that are greater than 127 then I always retrieve string with 2 bytes (in UTF-16).

I really want only 1-byte representation of every byte. How can I achieve that?

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
  • Possible duplicate of [Encoding problems when writing to the console in node.js](http://stackoverflow.com/questions/13735228/encoding-problems-when-writing-to-the-console-in-node-js) – nwellnhof Nov 25 '15 at 11:30
  • @nwellnhof, it's not the same problem. In the question you referenced the problem is about appearance of the output. But in my question I need to get String object in 1-byte encoding. – Michael Sivolobov Nov 25 '15 at 11:54
  • You can't change the encoding of JavaScript Strings. You'll need an API that accepts byte arrays. – nwellnhof Nov 25 '15 at 11:57
  • One of my programmers solved this problem someway. I really forgot to add his solution here. But in the application now all work fine. – Michael Sivolobov Nov 25 '15 at 11:59

1 Answers1

0

IconV could perhaps work

Something like (untested code!):

  var Buffer = require('buffer').Buffer;
  var Iconv  = require('iconv').Iconv;

  var iconv = new Iconv('UTF-16','latin1');

  bytes.forEach(function(byte){
      var bufferbyte = iconv.convert(byte);
  });
Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
ztripez
  • 664
  • 6
  • 24
  • I tried to use iconv but has a problem: no method will return string in needed encoding. Only byte representation. I need to send string to the terminal in latin1. This lib can't help here. – Michael Sivolobov Nov 10 '14 at 11:41