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

前端 未结 6 2021
猫巷女王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 03:00

    Try this:

    //Converts the upper nibble of a binary value to a hexadecimal ASCII byte.
    //For example, btohexa_high(0xAE) will return 'A'.
    unsigned char btohexa_high(unsigned char b)
    {
        b >>= 4;
        return (b>0x9u) ? b+'A'-10:b+'0';
    }
    
    
    //Converts the lower nibble of a binary value to a hexadecimal ASCII byte.
    //  For example, btohexa_low(0xAE) will return 'E'.
    
    
    unsigned char btohexa_low(unsigned char b)
    {
        b &= 0x0F;
        return (b>9u) ? b+'A'-10:b+'0';
    }
    

    And in main code:

    comand_mod=0xA1; //example variable
    Serial.print(btohexa_high(comand_mod));
    Serial.print(btohexa_low(comand_mod));
    

提交回复
热议问题