# Arduino Forum

## Using Arduino => Programming Questions => Topic started by: Chipakias on Nov 19, 2012, 09:21 pm

Title: How to convert a number to binary ?
Post by: Chipakias on Nov 19, 2012, 09:21 pm
I do not know how to convert a number to binary.

I have seen the way with Serial.print(x,bin) but what I want is a variable to take the binary and not just to print it.

For example :
When x is a random number
y = (x, BIN);
Title: Re: How to convert a number to binary ?
Post by: Arrch on Nov 19, 2012, 09:30 pm
Numbers are already stored in binary.

Perhaps you should give us a broader sense of what you are trying to do, rather than how you are trying to do it.
Title: Re: How to convert a number to binary ?
Post by: WizenedEE on Nov 19, 2012, 09:44 pm
Code: [Select]
`unsigned long convToBin(unsigned int x) {'  unsigned long ret = 0;  for (unsigned char i=0; i<sizeof(x)*8; i++) {    if (x & _BV(i)) ret += 10 * i;  }}`
untested
Title: Re: How to convert a number to binary ?
Post by: PeterH on Nov 19, 2012, 10:41 pm

I do not know how to convert a number to binary.

It's not clear what you are trying to do. My guess is that you are trying to produce an ascii string of '1' and '0' characters containing a binary textual representation of an integer, similar to what you'd get if you printed the number to the Serial port with binary format. Is that right?
Title: Re: How to convert a number to binary ?
Post by: dhenry on Nov 19, 2012, 11:26 pm
If you are talking about a binary string: 3 = "00000011" for example, the following does it:

Code: [Select]
`unsigned char *hex2binstr(unsigned char *str, unsigned char dat) {  unsigned char mask = 0x80;  do {    if (dat & mask) *str='1'; //the bit is 1    else *str='0'; //otherwise it is 0    str+=1; //increment the pointer    mask = mask >> 1; //shift mask  } while (mask);  return str;}`

It converts an unsigned char into an 8-char string.

You can then build on top of it routines that convert more complex structures into str.
Title: Re: How to convert a number to binary ?
Post by: dhenry on Nov 19, 2012, 11:56 pm
Here is what you can do to convert a short / long:

Code: [Select]
`//convert a char type to a binary stringunsigned char * uc2binstr(unsigned char * str, unsigned char dat) {...}//convert a short type to binary stringunsigned char * us2binstr(unsigned char *str, unsigned short dat) {  str=uc2binstr(str, dat >> 8); //convert msb first  str=uc2binstr(str, dat);  return str;}//convert a long type to binary stringunsigned char * ul2binstr(unsigned char *str, unsigned long dat) {  str=us2binstr(str, dat >> 16); //convert msw first  str=us2binstr(str, dat);  return str;}`

On longer types, you can simply use one of those routines successively, starting with the msb/msw first.
Title: Re: How to convert a number to binary ?
Post by: kowalski on Nov 20, 2012, 12:37 am
There are four functions available in the AVR library that you might want to use:

char* itoa    ( int  __val, char *  __s, int  __radix)

http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga4f6b3dd51c1f8519d5b8fce1dbf7a665

Passing the __radix parameter the value 2 will give you a binary character string.

Cheers, Mikael
Title: Re: How to convert a number to binary ?
Post by: guix on Nov 20, 2012, 06:17 am
Hello and welcome, maybe you just want to know how to write a number as binary?

By adding "0b" prefix (or "B" prefix, for bytes (8 bits) only), like this:

Code: [Select]
`int x = 0b0011000000111001; //binary notation for 12345byte y = B01111011;   //binary notation for 123byte z = 0b01111011; //also valid of course`

Title: Re: How to convert a number to binary ?
Post by: Chipakias on Nov 21, 2012, 02:51 pm

I do not know how to convert a number to binary.

It's not clear what you are trying to do. My guess is that you are trying to produce an ascii string of '1' and '0' characters containing a binary textual representation of an integer, similar to what you'd get if you printed the number to the Serial port with binary format. Is that right?

First of all, thank you all for the immediate correspondence and really sorry for mine late correspondence, but I work too much and the free time is little."

PeterH , Yes ! This is what I want. For example I have an integer x = 100 and I want another variable y to take the binary of 100 => "0 1 1 0 0 1 0 0". As I am a newbie I do not know many things. Please be as simplest and more detailed (regarding commands) as you can!
Title: Re: How to convert a number to binary ?
Post by: AWOL on Nov 21, 2012, 03:03 pm
Quote
For example I have an integer x = 100 and I want another variable y to take the binary of 100

Code: [Select]
`byte y = x;`
Title: Re: How to convert a number to binary ?
Post by: Chipakias on Nov 25, 2012, 06:19 pm

Quote
For example I have an integer x = 100 and I want another variable y to take the binary of 100

Code: [Select]
`byte y = x;`

Thank you for your answer but I am not sure that it works as when I create the program :

int x= 100;
void setup() {
Serial.begin(9600);
}
void loop() {
byte y = x;
Serial.println (y);
}

It prints 100. Why ?
Title: Re: How to convert a number to binary ?
Post by: Arrch on Nov 25, 2012, 06:22 pm

Thank you for your answer but I am not sure that it works as when I create the program :

int x= 100;
void setup() {
Serial.begin(9600);
}
void loop() {
byte y = x;
Serial.println (y);
}

It prints 100. Why ?

Because you told it to. What are you expecting it to do?

Serial.print() and Serial.println() convert 100 to '1', '0' and '0'. to print it to the screen. You can use Serial.write and it will send the binary value of 100 to the screen, which is 'd'.
Title: Re: How to convert a number to binary ?
Post by: PaulS on Nov 25, 2012, 06:43 pm
If you want to print the binary representation of the value, you have to tell Serial.print() that. Look at the documentation for how.
Title: Re: How to convert a number to binary ?
Post by: Chipakias on Nov 27, 2012, 12:09 pm

If you want to print the binary representation of the value, you have to tell Serial.print() that. Look at the documentation for how.

Yes I know. Serial.print(y,bin).

Something else I would like to know is about how can I separate a 16 bin value of a variable into
2 8bin variables. For example  x = 0B1111111100000001, I would like  y to be 0B11111111 and
z = 0B00000001 in order to send them with shiftout (using 2 shift registers) to another chip.
Title: Re: How to convert a number to binary ?
Post by: PaulS on Nov 27, 2012, 12:15 pm
Quote
Something else I would like to know is about how can I separate a 16 bin value of a variable into
2 8bin variables. For example  x = 0B1111111100000001, I would like  y to be 0B11111111 and
z = 0B00000001 in order to send them with shiftout (using 2 shift registers) to another chip.

So, you WERE able to find the reference page. Too bad you didn't spend just a little bit longer there. That's a page well worth spending more then 2 minutes at. You should at least familiarize your self with the available functions, even if you don't study things like highByte (http://arduino.cc/en/Reference/HighByte) and lowByte (http://arduino.cc/en/Reference/LowByte). Just knowing that they are there is useful.
Title: Re: How to convert a number to binary ?
Post by: AWOL on Nov 27, 2012, 12:28 pm
Quote
Yes I know. Serial.print(y,bin).

Close, but no cigar.
Title: Re: How to convert a number to binary ?
Post by: Chipakias on Nov 28, 2012, 10:32 am

Quote
Something else I would like to know is about how can I separate a 16 bin value of a variable into
2 8bin variables. For example  x = 0B1111111100000001, I would like  y to be 0B11111111 and
z = 0B00000001 in order to send them with shiftout (using 2 shift registers) to another chip.

So, you WERE able to find the reference page. Too bad you didn't spend just a little bit longer there. That's a page well worth spending more then 2 minutes at. You should at least familiarize your self with the available functions, even if you don't study things like highByte (http://arduino.cc/en/Reference/HighByte) and lowByte (http://arduino.cc/en/Reference/LowByte). Just knowing that they are there is useful.

You are right. I had to search more the reference page. It is in my plans. Thank you very much for your response. You helped me a lot and I understood many things!
Title: Re: How to convert a number to binary ?
Post by: Chipakias on Nov 28, 2012, 10:50 am

Quote
Yes I know. Serial.print(y,bin).

Close, but no cigar.

You also helped me a lot, it tooks me enough time to understand byte behavior and how to print it . Also sorry for my late response.