View previous topic :: View next topic |
Author |
Message |
raman00084@gmail.com
Joined: 28 Jul 2014 Posts: 38
|
16 bit integer to binary |
Posted: Fri Jan 01, 2021 9:11 am |
|
|
I want to convert a 16bit number to binary and store the value in an array.
What is the best and simple way to do this? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 01, 2021 9:20 am |
|
|
Where do your 16-bit numbers come from, and what format are they in ?
Are they ASCII numbers in a string ? Are they in BCD format ?
Or, are they already in binary and you don't know it ?
What device is sending these numbers to you ? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19490
|
|
Posted: Fri Jan 01, 2021 9:42 am |
|
|
A 'bit' is a binary digit. So if you have a '16bit number', then it already is
binary!.... Internally things are stored in binary....
Do you mean you want to output it as a binary text string?. If so, look
at the itoa function. |
|
|
raman00084@gmail.com
Joined: 28 Jul 2014 Posts: 38
|
|
Posted: Fri Jan 01, 2021 10:30 pm |
|
|
Ttelmah wrote: | A 'bit' is a binary digit. So if you have a '16bit number', then it already is
binary!.... Internally things are stored in binary....
Do you mean you want to output it as a binary text string?. If so, look
at the itoa function. |
for example i have i int16 number
unsigned int16= 62261;
the binary for the above is 1111 0011 0011 0101
now how can i convert 62261 to 1111 0011 0011 0101 and store the
1s and 0s in an array |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19490
|
|
Posted: Sat Jan 02, 2021 2:16 am |
|
|
So you actually want an array of numbers 0/1, not ascii text?. itoa
converts to ASCII.
You would just have to code it. Something like:
Code: |
int1 result[16];
void valtobin(unsigned int16 from)
{
int8 index=0;
for(index=0;index<15;index++)
{
if (from & 1/
result[index]=1;
else
result[index]=0;
from/=2;
}
}
//If you then call valtobin(62261);
|
result[0] will be the bottom bit result[1] the next etc..
In fact you could probably do this directly by creating a union. So:
Code: |
union {
int1 bits[16];
unsigned int16 whole;
} getbits;
getbits.whole=62261;
//should then give getbits.bits[0] withh the least significant bit etc...
|
|
|
|
raman00084@gmail.com
Joined: 28 Jul 2014 Posts: 38
|
|
Posted: Sat Jan 02, 2021 4:28 am |
|
|
Ttelmah wrote: | So you actually want an array of numbers 0/1, not ascii text?. itoa
converts to ASCII.
You would just have to code it. Something like:
Code: |
int1 result[16];
void valtobin(unsigned int16 from)
{
int8 index=0;
for(index=0;index<15;index++)
{
if (from & 1/
result[index]=1;
else
result[index]=0;
from/=2;
}
}
//If you then call valtobin(62261);
|
result[0] will be the bottom bit result[1] the next etc..
In fact you could probably do this directly by creating a union. So:
Code: |
union {
int1 bits[16];
unsigned int16 whole;
} getbits;
getbits.whole=62261;
//should then give getbits.bits[0] withh the least significant bit etc...
|
|
You have done one mistake it should be index<=15, you have missed the =
for(index=0;index<15;index++);
Thanks for your help it is working now. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19490
|
|
Posted: Sat Jan 02, 2021 9:40 am |
|
|
Honestly the union method is far better. Uses a huge amount less space and
time. |
|
|
|