how to convert an integer to binary in Arduino?

wvmarle:
Works for any A where B = 10 (and every B where A = 10).

I could not sleep the other night and thought about this - it did not help with the sleep :slightly_frowning_face:

AB=BA it’s algebra Jim, but not as we know it. The “trick” works because although the ‘B’ on both sides of the equation look the same they actually have different numeric values as they are in different bases.

Any base written in that base is always 10. As @wvmarle wrote the trick always works as long as at least one of the subscripts is 10. However it works whatever base the subscript is in as long as it is 10 e.g.

Binary: 1110 = 1011 = 3 decimal

Octal: 1110 = 1011 = 9 decimal

Hexadecimal: 1110 = 1011 = 17 decimal

Hexadecimal: 1F10 = 101F = 31 decimal

Curious, but I am not sure what use it has.

ardly:
Curious, but I am not sure what use it has.

I don't think it has any use other than to help fellow forum members lose a night of sleep over it :slight_smile:

That went round and round in my head!

Anyway getting back to the subject of the OP maybe this will make things clear;

#include <iostream>

using namespace std;
bool convert(char chr, unsigned char* value);

#define ARRAYSIZE 19

int main()
{
    unsigned char bits;
    char chr,ASCIIChars[ARRAYSIZE]=
    {
        '!','0','1','2','3','4','5','6','7','8','9','?','A','B','C','D','E','F','a'
    };

    for(int i=0;i<ARRAYSIZE;++i)
    {
        chr=ASCIIChars[i];
        if(convert(chr,&bits))
        {
            printf("ASCII Character '%c' HEX 0x%02X Decimal %02d represents numeric value HEX 0x%02X decimal %2d\n",chr,chr,chr,bits,bits);
        }
        else
        {        
            printf("ASCII Character '%c' HEX 0x%02X Decimal %02d does not represent a hexadecimal digit\n",chr,chr,chr);
        }
     }
     return 0;
}

bool convert(char chr, unsigned char* value)
{
    // Convert ASCII characters representing hexadecimal digits into their numeric value
    // only convert uppercase alphabetic characters i.e. A...F not a...f
   
    //Return    TRUE     if the character represents a valid hexadecimal digit
    //             FALSE   if the character does not represent a hexadecimal digit
    
    bool result;

    result=false;  
    if(chr>='0'&&chr<='9')
    {       
        *value=chr-'0';      
        result=true;
    }
    else if(chr>='A'&&chr<='F')
    {      
        *value=chr-'A'+10;      
        result=true;
    }
    return(result);
}

The code can be executed online here;
https://www.tutorialspoint.com/compile_cpp_online.php

The OP should trap invalid characters if he is transmitting the Hex in case of corruption.

I don’t have an Arduino to hand.

Output is;

ASCII Character '!' HEX 0x21 Decimal 33 does not represent a hexadecimal digit
ASCII Character '0' HEX 0x30 Decimal 48 represents numeric value HEX 0x00 decimal  0
ASCII Character '1' HEX 0x31 Decimal 49 represents numeric value HEX 0x01 decimal  1
ASCII Character '2' HEX 0x32 Decimal 50 represents numeric value HEX 0x02 decimal  2
ASCII Character '3' HEX 0x33 Decimal 51 represents numeric value HEX 0x03 decimal  3
ASCII Character '4' HEX 0x34 Decimal 52 represents numeric value HEX 0x04 decimal  4
ASCII Character '5' HEX 0x35 Decimal 53 represents numeric value HEX 0x05 decimal  5
ASCII Character '6' HEX 0x36 Decimal 54 represents numeric value HEX 0x06 decimal  6
ASCII Character '7' HEX 0x37 Decimal 55 represents numeric value HEX 0x07 decimal  7
ASCII Character '8' HEX 0x38 Decimal 56 represents numeric value HEX 0x08 decimal  8
ASCII Character '9' HEX 0x39 Decimal 57 represents numeric value HEX 0x09 decimal  9
ASCII Character '?' HEX 0x3F Decimal 63 does not represent a hexadecimal digit
ASCII Character 'A' HEX 0x41 Decimal 65 represents numeric value HEX 0x0A decimal 10
ASCII Character 'B' HEX 0x42 Decimal 66 represents numeric value HEX 0x0B decimal 11
ASCII Character 'C' HEX 0x43 Decimal 67 represents numeric value HEX 0x0C decimal 12
ASCII Character 'D' HEX 0x44 Decimal 68 represents numeric value HEX 0x0D decimal 13
ASCII Character 'E' HEX 0x45 Decimal 69 represents numeric value HEX 0x0E decimal 14
ASCII Character 'F' HEX 0x46 Decimal 70 represents numeric value HEX 0x0F decimal 15
ASCII Character 'a' HEX 0x61 Decimal 97 does not represent a hexadecimal digit