How to `Serial.print()` “full” hexadecimal bytes?

前端 未结 6 1962
猫巷女王i
猫巷女王i 2021-01-18 02:17

I am programming Arduino and I am trying to Serial.print() bytes in hexadecimal format \"the my way\" (keep reading for more information).

That is, by u

6条回答
  •  离开以前
    2021-01-18 02:59

    Use sprintf to print into a buffer (two chars per byte + null terminator):

    byte byte1 = 0xA2;
    byte byte2 = 0x05;
    byte byte3 = 0x00;
    char s[7];
    sprintf(s, "%02x\n%02x\n%02x", byte1, byte2, byte3);
    Serial.println(s);
    

    Added new lines in between to get each on new line. About '%02x', the % means here comes formatting information, 0 means to pad with 0, 2 means pad input until 2 characters wide and x means give me this as hexadecimal.

    For other formatting options see http://linux.die.net/man/3/sprintf

提交回复
热议问题