3

Having a problem here. I recieve data from hardware as HEX numbers. I use this to make them into strings(it is intended):

arr.push(data.charCodeAt(0).toString(16))

It works well. For example I recieve 0x00 and this code correctly adds a '0' string to the array.

But there is a problem. JS automatically understands these codes as symbols. And when I try 0xAA or 0x80 I recieve FFFD. As I understand. it means that there is no such symbol in utf-8.

How do I make js understand my data as hex-numbers, but not as symbol codes?

Edit: I figured out that my problem was in my wrong way of using the library which recieved data from hardware.

stdnik
  • 133
  • 1
  • 10
  • you aren't showing any symbols, you are showing the hex code for bytes. take off the `.toString(16)` if you want raw chars – dandavis May 29 '18 at 18:15
  • Possible duplicate of [How to convert decimal to hex in JavaScript?](https://stackoverflow.com/questions/57803/how-to-convert-decimal-to-hex-in-javascript) – Gerardo BLANCO May 29 '18 at 18:16

3 Answers3

0

Just convert the Hex number to decimal

yourNumber = parseInt(hexString, 16);
Sacha
  • 322
  • 1
  • 8
0

To convert hexadecimal to decimal you can use the next function:

parseInt(hexValue, 16)

Hope this helps :)

data = '0XAA'
data1 = '0X80'
console.log( parseInt(data, 16))
console.log( parseInt(data1, 16))
Gerardo BLANCO
  • 5,590
  • 1
  • 16
  • 35
  • can you give an example of your data – Gerardo BLANCO May 29 '18 at 23:19
  • and the typeof that data – Gerardo BLANCO May 29 '18 at 23:19
  • example of data: ��]�k���] typeof: string What I sent from hardware: 0xAA 0x81 0x5D 0x00 0xE8 0xBD 0x74 0x10 0x00 0x12 – stdnik May 30 '18 at 01:41
  • ��]�k���] Thats want you log before you do any change? ``]`` in ascii is ``0x5D`` but ``0x00`` in ascii is equal to ``null`` not to ``k``. You need to find a different way to send your data from the hardware. � this will not give you any information. https://apps.timwhitlock.info/unicode/inspect?s=%EF%BF%BD%EF%BF%BD%5D%EF%BF%BDk%EF%BF%BD%EF%BF%BD%EF%BF%BD – Gerardo BLANCO May 30 '18 at 02:36
0

Okay, I figured out that my problem was in a library I used to recieve data

stdnik
  • 133
  • 1
  • 10