CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

16 bit integer to binary

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

16 bit integer to binary
PostPosted: Fri Jan 01, 2021 9:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 01, 2021 9:20 am     Reply with quote

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: 19520

View user's profile Send private message

PostPosted: Fri Jan 01, 2021 9:42 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 01, 2021 10:30 pm     Reply with quote

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: 19520

View user's profile Send private message

PostPosted: Sat Jan 02, 2021 2:16 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Jan 02, 2021 4:28 am     Reply with quote

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: 19520

View user's profile Send private message

PostPosted: Sat Jan 02, 2021 9:40 am     Reply with quote

Honestly the union method is far better. Uses a huge amount less space and
time.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group