Strange result of String.fromCharCode

Posted on
  • String.fromCharCode seems to return strange values for codes below 8:
    String.fromCharCode(8) -> "\b" (OK) String.fromCharCode(7) -> "\713" (???)
    Although String.fromCharCode(7).length returns 1 it doesn't seem to be the same as "\x07".
    What's going on here?

  • Sun 2019.03.17

    @Steffen, what version of Espruino is flashed and on which device?

    process.env

    Please post

  • process.env
    ={
    VERSION: "2v01",
    GIT_COMMIT: "748a4d3",
    BOARD: "ESPRUINOWIFI",
    FLASH: 524288, RAM: 131072,
    SERIAL: "65005000-0b513532-39333638",
    CONSOLE: "USB",
    MODULES: "Flash,Storage,hea" ... ",neopixel,Wifi,AT",
    EXPTR: 536871128 }

  • Please could you provide the exact code that gives you the error? I just tried this and it works fine for me with identical firmware on the same device:

    >process.env
    ={
      VERSION: "2v01",
      GIT_COMMIT: "748a4d3",
      BOARD: "ESPRUINOWIFI",
      FLASH: 524288, RAM: 131072,
      SERIAL: "3d002100-0b513532-39333638",
      CONSOLE: "USB",
      MODULES: "Flash,Storage,hea" ... ",neopixel,Wifi,AT",
      EXPTR: 536871128 }
    >String.fromCharCode(7)
    ="\7"
    
  • You are right, it works perfectly well on a clean device.
    It seems like passing a wrong argument (the '\x07' string) causes the error:

    >String.fromCharCode('\x07')  // wrong argument, but no complaint
    ="\0FD"
    >String.fromCharCode(7)     // correct argument, but extra chars
    ="\7FD"
    >String.fromCharCode(8)    // correct argument (above 8 every value gives the correct result)
    ="\b"
    >String.fromCharCode(7)    // correct argument below 8, always with extra chars
    ="\7FD"
    

    (It was pretty hard to find out how to reproduce the effect.)

  • Wow, thanks for narrowing this down. Turns out it's to do with outputting a hex value under 32 but above 7, and THEN outputting under 8.

    This reliably does it:

    >String.fromCharCode(0x1f)
    ="\x1F"
    >String.fromCharCode(7)
    ="\71F"
    

    It's just been fixed!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Strange result of String.fromCharCode

Posted by Avatar for Steffen @Steffen

Actions