decimal integer to binary

ok so i have an integer called Temp
it contains the current temperature for example 20
i want to convert 20 a decimal number into 00010100 its binary form
i want each binary digit to be separated in to a char array

any help please?

A for loop and the bitRead() function would be a start.

What are you trying to achieve by doing this? Is Temp an int variable?

Rough question, a rough answer.

An assumption that the buffer may be printed.

If not shorten the buffer length by 1 and remove the ticks from around ternary expressions values 1 and 0.

#include <limits.h>

const size_t    BIT_COUNT               = CHAR_BIT * sizeof(Temp)
size_t          bit_mask                = (1 << (BIT_COUNT - 1));
char            buffer[BIT_COUNT + 1]   = { 0 };
char*           pBuffer                 = buffer;
do
{
    *pBuffer++ = (Temp & mask) ? '1' : '0';
} while (mask >>= 1);

cheesemarathon:
ok so i have an integer called Temp
it contains the current temperature for example 20
i want to convert 20 a decimal number into 00010100 its binary form
i want each binary digit to be separated in to a char array

any help please?

Why? What are you trying to build?
Is this an XY problem? http://xyproblem.info/

byte temp = 20;
char myArray[8];

void setup() 
{
  Serial.begin(9600);
  bitPrint(temp);
}

void loop() 
{
}

void bitPrint(byte temp)
{
  Serial.print("0b");
  for (byte i = 0; i < 8; i++)
  {
    Serial.print(bitRead(temp, i)? "1" : "0" );
    myArray[i] = bitRead(temp, i)? "1" : "0";
  }
  Serial.println();
}

output:

0b00101000

@BulldogLowell

While temp is defined as 20, your output says 40. You got an extra zero on the end there somehow.

Do we have to put it in an array? The OP didn't say. If you just want to display it, how about this?

byte temp = 20;

void setup(){
     Serial.begin(9600);
     Serial.println(temp , BIN);
}

void loop(){}
10100

Delta_G:
@BulldogLowell

While temp is defined as 20, your output says 40. You got an extra zero on the end there somehow.

Wait, I see. You printed it backwards. Your for loop should run 7 to 0 instead of 0 to 7.

he he

oops!

[code]
void setup()
{
  /*
    // put your setup code here, to run once:
    ok so i have an integer called Temp
    it contains the current temperature for example 20
    i want to convert 20 a decimal number into 00010100 its binary form
    i want each binary digit to be separated in to a char array
    }
  */
  Serial.begin(115200);
  int Temp = 20;
  char cArray[8]; // assuming  max 8 bits
  int i = 0;
  do
  {
    Serial.println(Temp, BIN);
    cArray[i] = Temp & 1;    // mask bit 0 into array 
    Serial.println(cArray[i++], BIN);
    Temp >>=  1;                 // next bit 
  } while (Temp);                // suppress leading zeroes 
}
void loop() {
  // put your main code here, to run repeatedly:
}

[/code]

That code also gets it backwards.

20 in binary is 10100 but the array contains now 00101

10100
0
1010
0
101
1
10
0
1
1

“want each binary digit to be separated in to a char array”

So what is “backwards” ?
OP does not specify that the array should “read” as anything, just contains the Temp.

0 cArray[0] first member of array contains LSD etc.

Delta_G:
That code also gets it backwards.

20 in binary is 10100 but the array contains now 00101

10100

0            cArray[0]
1010
0            cArray[1]
101
1            cArray[2}
10
0
1
1

Right, your first entry is the least significant bit.

Add a line in there to print out cArray and you'll see. If you want this printed right you'll have to use a for loop and print cArray[7] first and work down to cArray[0] where the least significant bit is. But you've suppressed leading zeroes, so unless cArray is initialized to all 0's who knows what's in cArray[7].

Delta_G:
@BulldogLowell
Do we have to put it in an array? The OP didn't say.

Says right there near the bottom of his post.

But what is doesn't say is whats to be done with the result!

The code fragment I provided works for any native type from byte to long with the limit being the length of size_t.

EDIT: It also has an advantage of not modifying the original variable/constant we're try to print the binary representation of out from under the rest of the program.

Ok sorry i didnt provide enough information
i have a temperature sensor and i get the current temp from that and store it like this:
int Pressure = bmp.readPressure();
i then want to send this temperature in its binary form over a radio link using the virtual wire library.
to do this i need each digit of the binary number separated in a char array

Does this help and will it change any code you've already suggested?
Any more info needed?

cheesemarathon:
Ok sorry i didnt provide enough information
i have a temperature sensor and i get the current temp from that and store it like this:
int Pressure = bmp.readPressure();
i then want to send this temperature in its binary form over a radio link using the virtual wire library.
to do this i need each digit of the binary number separated in a char array

Does this help and will it change any code you've already suggested?
Any more info needed?

You are using a function called readPressure() to read the temperature?

Also, I have a feeling you misunderstand "in its binary form". Please provide a link to the instructions you are trying to follow.

haha oops copied the wrong code.
so the line that gets the temp is:

int Temp = bmp.readTemperature();

so for example Temp could now equal 20
i want a char array lets call it TEMP[8] which contains 20, the current temp, in binary eg 00010100
so...
TEMP[1] = 0
TEMP[2] = 0
TEMP[3] = 0
TEMP[4] = 1
TEMP[5] = 0
TEMP[6] = 1
TEMP[7] = 0
TEMP[8] = 0

does that help?

cheesemarathon:
i want a char array lets call it TEMP[8] which contains 20, the current temp, in binary eg 00010100
so…
TEMP[1] = 0
TEMP[2] = 0
TEMP[3] = 0
TEMP[4] = 1
TEMP[5] = 0
TEMP[6] = 1
TEMP[7] = 0
TEMP[8] = 0

does that help?

arrays start at zero, and the first bit in a byte is the zero-eth bit:

Did you try what I posted (corrected to print correctly):

byte temp = 20;
char myArray[8];

void setup() 
{
  Serial.begin(9600);
  bitPrint(temp);
}

void loop() 
{
}

void bitPrint(byte temp)
{
  Serial.print("0b");
  for (byte i = 0; i < 8; i++)
  {
    Serial.print(bitRead(temp, 7-i)? "1" : "0" );
    myArray[i] = bitRead(temp, i)? "1" : "0"; //<<<<<<<<<<n your array populates here
  }
  Serial.println();
}

the order may be changed by manipulating the indicated line

@cheesemarathon, is there a micro-controller, Arduino or otherwise, at the other end?

Create a union of int and a char array. Assign the temperature to the int part of the array, and pass the char part to virtual wire. This is the most efficient way.