NordicSemiconductor / nrf-intel-hex

Yet another parser for "Intel HEX" files.
BSD 3-Clause "New" or "Revised" License
27 stars 8 forks source link

Why hexadecimals values are changed to 0? #15

Closed XaviLC closed 6 years ago

XaviLC commented 6 years ago

THe function asHexString() does not map the hexadecimals values properly and they are converted to 0's?

let inHex = new MemoryMap();
console.log("ABCDEF123".split(""))
let inHexBytes = new Uint8Array("ABCDEF123".split(""));
inHex .set(hexAddress, inHexBytes ); 
console.log("Serial number in .hex\n"+ inHex .asHexString());

Gives as ouput the following, where the hex string "ABCDEF123" is not parsed at all and ABCDEF values are set to zeros...

[ 'A', 'B', 'C', 'D', 'E', 'F', '1', '2', '3' ]
Serial number in .hex
:020000040007F3
:09FC0000000000000000010203F5
:00000001FF
IvanSanchez commented 6 years ago

This is not a bug in asHexString, but rather a GIGO caused by the way you're instantiating your Uint8Array.

The Uint8Array constructor takes an array of Numbers as its first parameter. If the elements in that array are not instances of Number, they will be casted into a decimal number as usual. Any letter will be casted to NaN, which will turn into a zero.

You can check this yourself by running the instantiation standalone:

> new Uint8Array("ABCDEF123".split(""));
Uint8Array(9) [ 0, 0, 0, 0, 0, 0, 1, 2, 3 ]

And, if you look at your output carefully, you'll see that asHexString is just outputting the values stored in such an Uint8Array:

:09FC0000000000000000010203F5

or

:09FC0000   00 00 00 00 00 00 01 02 03   F5

I will also call your attention over this particular line of code:

https://github.com/NordicSemiconductor/nrf-intel-hex/blob/490d9395eb4cb64bee5dbd1ac39dc55586a4de01/intel-hex.js#L201

As this is not a bug in nrf-intel-hex itself, I'm closing this.