2

I have to send data to a connected object one byte chars. So I convert integer data to ASCII with the help of String.fromCharCode().

As long as the integer is less than 127, then the caracter is stored in one byte. But beyond 128, which is normal, the data consists in more than one byte.

How can I deal with that ?

For sure I checked the number of bytes using encodeURI(s).split(/%..|./).length - 1

I tried this SO post but it did not help much

Community
  • 1
  • 1
epsilones
  • 11,279
  • 21
  • 61
  • 85
  • 1
    Could you explain in more detail?, it's not really obvious what your trying to do. What connected object, are you receiving data in integer form, and want that converting into a string?.. – Keith Dec 15 '16 at 13:50
  • 1
    I've read it 3 times and still don't get what it is you want to do here. – iHasCodeForU Dec 15 '16 at 13:52

1 Answers1

0

The standard that W3C and IETF have pushed for UTF-8 as the standard for string data in web designs. Therefore sending one character in one byte is not possible for the set of characters that are generally supported. This may be something close to what you might want though.

var utf_8 = unescape(encodeURIComponent(string));
var byteArray = [];
for (var i = 0; i < utf_8.length; ++ i)
    byteArray.push(utf_8.charCodeAt(i));
Douglas Daseeco
  • 3,475
  • 21
  • 27