escape sequence for special hex values

Posted on
  • Hi

    I'm assembling a string of special characters e.g.:

     print("\x10\x20\x10");
    

    While it works flawlessly on the Espruino the IDE marks \x20 as unnecessary escapement. Note, \x10 is not marked. I assume its a bug in the IDE?

  • \x20 is unneccessary escaping, because it's a normal space, hence doesn't need an escape.

    Ascii code 0x20 (32) is space.

  • Great catch! I haven't looked at it from that angle.

    However, I'm using escape sequences like the one above to include parts (2 to 32 bytes) of a binary protocol . They are sent out on the serial interface.

    Replacing all printable characters in the sequence would be cumbersome and error prone. Can such warnings be disabled?

  • I'm afraid they can only be disabled by tweaking the IDE's code at the moment... I guess maybe a more readable (if marginally less efficient) way of writing it might be:

    String.fromCharCode(0x10,0x20,0x10)
    

    But it depends where you're sending the data. If it's down SPI/I2C then you can just send an array of values rather than a string. You can even do it with the Serial ports using write:

    Serial1.write([0x10,0x20,0x10]) 
    
  • The alternative forms look nice! Thank your for the suggestion.

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

escape sequence for special hex values

Posted by Avatar for AntiCat @AntiCat

Actions