You are reading a single comment by @allObjects and its replies. Click here to read the full conversation.
  • Assuming d is an array of numbers / bytes - not String or array of strings:

    Reference doc http://www.espruino.com/Reference#l_Arra­y_toString says:

    Parameter - radix: unused

    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:

    [11,12,9].toString(16)
    --> "11,12,9,32"
    [11,12,9].forEach(function(b){ console.log(b.toString(16)); })
    VM3935:1--> b
    VM3935:1--> c
    VM3935:1--> 9
    VM3935:1--> 20
    --> undefined
    [11,12,9,32].reduce(function(r,b){ return r+b.toString(16); },"")
    --> "bc920"
    [11,12,9,32].reduce(function(r,b){ return r+("0"+b.toString(16)).substr(-2); },"")
    --> "0b0c0920"
    

    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.

    >reset()
    =undefined
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v00 (c) 2018 G.Williams
    >[11,12,9,32].reduce(function(r,b){ return r+("0"+b.toString(16)).substr(-2); },"")
    ="0b0c0920"
    > 
    

    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:

    >new Uint8Array(E.toArrayBuffer("012 abc"))
    =new Uint8Array([48, 49, 50, 32, 97, 98, 99])
    >(new Uint8Array(E.toArrayBuffer("012 abc"))).reduce(function(r,b){ return r+("0"+b.toString(16)).substr(-2); },"")
    ="30313220616263"
    > 
    

    ...and with variable d being the String:

    console.log((new Uint8Array(E.toArrayBuffer(d))).reduce(f­unction(r,b){ return r+("0"+b.toString(16)).substr(-2); },""));
    

    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.

About

Avatar for allObjects @allObjects started