You apply .toString() to the array object and not to the individual items in the array. If you do so, it just works. Did some messing around in the browser debugger console - you get there by ctrl-click and inspect, where you can enter JavaScript expressions when not being connected to an Espruino board:
Use Array.reduce(...) and you get what you are looking for... just make sure that you always get a 2-digit value for values less than 16 when talking hex, otherwise you cannot read it 'back' (talking octal, you need to bump it up to 3-digit).
Just validated in Espruino (on an Espruino-Wifi w/ Espruino 2v00). What though is clear is that the array elements have to be numbers / bytes, and NOT Strings or Chars.
BUTd is not an array of numbers or bytes, as I wrongly assumed: data return from Serial is always a String... where as from others, such as I2C, SPI, etc. it is a byte array...
THEREFORE , we have to get a byte array from the string. This is the final answer:
PS: @Gordon, could this parameter - if present - be used to trigger the alternate interpretation which is applying .toString(...) to the individual elements and still return a string rather than to the array as a whole? ...and even extend it to work on a String? ...I know, it is not standard JavaScript... so better not doing it, but finding other ways that to it fast.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Assuming
d
is an array of numbers / bytes - not String or array of strings:Reference doc http://www.espruino.com/Reference#l_Array_toString says:
Referred-to detail reference on MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString - does not have any parameter for
Array.toString()
. May be that having a parameter is something @Gordon has in mind for future purposes......on the other hand it make perfect sense:
You apply .toString() to the array object and not to the individual items in the array. If you do so, it just works. Did some messing around in the browser debugger console - you get there by ctrl-click and inspect, where you can enter JavaScript expressions when not being connected to an Espruino board:
Use
Array.reduce(...)
and you get what you are looking for... just make sure that you always get a 2-digit value for values less than 16 when talking hex, otherwise you cannot read it 'back' (talking octal, you need to bump it up to 3-digit).Just validated in Espruino (on an Espruino-Wifi w/ Espruino 2v00). What though is clear is that the array elements have to be numbers / bytes, and NOT Strings or Chars.
BUT
d
is not an array of numbers or bytes, as I wrongly assumed: data return from Serial is always a String... where as from others, such as I2C, SPI, etc. it is a byte array...THEREFORE , we have to get a byte array from the string. This is the final answer:
...and with variable
d
being the String:This is for hex... the others are similar.
PS: @Gordon, could this parameter - if present - be used to trigger the alternate interpretation which is applying
.toString(...)
to the individual elements and still return a string rather than to the array as a whole? ...and even extend it to work on a String? ...I know, it is not standard JavaScript... so better not doing it, but finding other ways that to it fast.