 # how to convert an integer to binary in Arduino?

I have been trying with different data types and nothing is working.

I am working with I2C communication in Arduino mega to Arduino uno. And I tried sending sample data and it was successful. The main agenda is sending 64 bits via I2C. I want to send in HEX and receive in HEX and i want to process as BINARY. Can anyone help me out in this ?

I have been trying with different data types and nothing is working.

How about posting what you have tried and describing what does not work as you want ?

I want to send in HEX and receive in HEX and i want to process as BINARY

HEX representation of numbers is only a way of making the value easier to read for humans. All values in the program are inherently binary behind the scenes. Unless, of course, you want to send and receive a series of characters representing the HEX value

The processor knows only binary.
Hex is just binary for grown-ups.

I want to send in HEX and receive in HEX

The decimal number 1110 can be written as C16 in hexadecimal or 10111 in binary.

When you say that you want to send and receive in “HEX” I suspect you mean that you want to send and receive printing ASCII characters which represent the hexadecimal equivalent of a number.

The ASCII character ‘C’ actually has a numeric value which is 64 decimal , 43 hexadecimal and 0100 0011 binary.

You probably want to transmit the ASCII characters ‘0’, ’1’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, ’9’, ’A’, ’B’, ’C’, ’D’, ’E’, ’F’ to represent hexadecimal digits.
When you receive a character you need to decide which ASCII character it is then use the actual hex number it represents to calculate your overall number.

I hope that makes sense.

1110 is B16 and 10112. (also 0138)

1110 is B16 and 10112. (also 0138)

Damn, well spotted, I should have read my post more carefully before sending.

AWOL:
1110 is B16 and 10112. (also 0138)

For better confusion: 1110 = 1011

wvmarle:
For better confusion: 1110 = 1011  I had a version of Forth many years ago where you could set what number base it worked in. Whenever you asked it to print out the current number base it always printed 10, no matter what you had previously set it to.

I used to ask students to explain why on the exam paper.

That's evil if you don't get that it's printed IN THAT BASE rather than base-10 :-)

For better confusion: 1110 = 1011

My brain is on strike, are there any other pairs of bases where you can write AB=BA ?

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

I am using this code to convert hex to binary. In this code I have initialized a sample character array with 4 characters, but in real scenario, the code would have more than 20 characters.

So i have used a separate function to convert the HEX to binary. Another function to separate the digits. and the code returns garbage value. can anyone help me in this ?

here is the code:

void setup() {
Serial.begin(9600);

}

void loop()
{
int *first_bits_upper = convert_digits(first_byte_upper);
for(int x =0; x <= 3; x++)
{
first_bits_upper++;
}
//Serial.println("");

}

int* convert_digits(unsigned int byte_value)
{
int converted_digits = {0,0,0,0,0};
int remaining_digits;
for(int y=0; y<=3; y++)
{
converted_digits[y] = byte_value % 10;
remaining_digits = byte_value / 10;
byte_value = remaining_digits;
}
}

unsigned long convert_binary(char a)
{
unsigned long converted;
if (a == ‘2’)
{
converted = 0010;
return converted;
}
if (a == ‘A’)
{
converted = 1010;
return converted;
}
if (a == ‘3’)
{
converted = 0011;
return converted;
}
if (a == ‘8’)
{
converted = 1000;
return converted;
}
}

can anyone help me in this ?

Not until you follow a few simple instructions on how to use code tags.

`````` if (a == '3')
{
converted = 0011;
``````

for example,

I am getting a character value('0' to 'F') from a stored array, lets say I am getting value of 'A'.

I need to convert that into 1 1 1 1 in an integer array of size 4.

So I have written a function that uses pointer to return the memory location of the converted bits.

But I am not getting what i actually want

I am getting a character value('0' to 'F') from a stored array, lets say I am getting value of 'A'.

I need to convert that into 1 1 1 1

That don't make no sense.

You're creating an int of that value, not a string (or char array).

``````unsigned long converted;
``````

Start by changing that to

``````char converted;
``````

and add quote marks around the digits. Change the rest of your code accordingly.

``````    converted = 0010;
return converted;
``````

you can completely remove the `converted` variable, it's not used anyway, and change this to

``````    return "0010";
``````

I think some standard library functions can do the conversion for chars or for string, just not for the whole 64bit number on Uno.

printf, scanf, strtol, itoa

Most of those go from string to number, OP wants to go the other way.

`sprintf()` may indeed work in this case, rather than printf, as it returns a string.