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

Problem to convert array of bytes to array of bits

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

Problem to convert array of bytes to array of bits
PostPosted: Wed Sep 11, 2013 2:08 am     Reply with quote

Buenos días:

Estoy intentando hacer un programa donde tengo 19 bytes en una cadena (int telemando1[19]) y los intento leer y meter en otra bit a bit (int1 telemandobits[152]). El objetivo es que si tengo en la primera cadena los valores 8,7,.... pasen a la segunda cadena con los valores binarios 0,0,0,0,1,0,0,0, 0,0,0,0,0,1,1,1, ...

El código que tengo es:

Code:

   int telemando1[19];
   int1 telemandobits[152];
   int i,z;

     for (i=0;i==151;i++) //Aqui inicializo la cadena
     {
         telemandobits[i]=0;
     }

        //Estos son los datos que quiero pasar a la otra cadena bit a bit
         telemando1[18]=8;
         telemando1[17]=7;
         telemando1[16]=6;
         telemando1[15]=5;
         telemando1[14]=4;
         telemando1[13]=12;
         telemando1[12]=0;
         telemando1[11]=1;
         telemando1[10]=17;
         telemando1[9]=255;
         telemando1[8]=255;
         telemando1[7]=255;
         telemando1[6]=255;
         telemando1[5]=255;
         telemando1[4]=255;
         telemando1[3]=255;
         telemando1[2]=255;
         telemando1[1]=0;
         telemando1[0]=0;

         //Aquí creo una cadena de bits con todos los datos
         for (i=18;i==0;i--)
         {  for (z=0;z==7;z++)
              { 
                 telemandobits[((i+1)*8)-(8-z)]=(int1) telemando1[i];
                 telemando1[i]>>=1; // x>>=y, is the same as x=x>>y
              }
         }


Luego leo la cadena telemandobits[] y no tengo los valores 0 y 1 esperados. La verdad es que creo que tiene que ser una tontería que no veo, pero me estoy volviendo loco. ConfusedConfused
Alguna sugerencia??

Gracias
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 2:11 am     Reply with quote

Format are as follow for for loop

Code:
for (i=0; i<= 151, i++) {


Regards
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 2:16 am     Reply with quote

Sorry Alan, I don´t understand
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 2:33 am     Reply with quote

In English better

I need pass bytes in array "int telemando1[19]" to another array "int1 telemandobits[152]" bit to bit. If in the first array bytes are 8,7,6, ... I need in the second array 0,0,0,0,1,0,0,0, 0,0,0,0,0,1,1,1, 0,0,0,0,0,1,1,0, ....

Code:
  int telemando1[19];
   int1 telemandobits[152];
   int i,z;

     for (i=0;i==151;i++) //Aqui inicializo la cadena
     {
         telemandobits[i]=0;
     }

        //Estos son los datos que quiero pasar a la otra cadena bit a bit
         telemando1[18]=8;
         telemando1[17]=7;
         telemando1[16]=6;
         telemando1[15]=5;
         telemando1[14]=4;
         telemando1[13]=12;
         telemando1[12]=0;
         telemando1[11]=1;
         telemando1[10]=17;
         telemando1[9]=255;
         telemando1[8]=255;
         telemando1[7]=255;
         telemando1[6]=255;
         telemando1[5]=255;
         telemando1[4]=255;
         telemando1[3]=255;
         telemando1[2]=255;
         telemando1[1]=0;
         telemando1[0]=0;

         //Aquí creo una cadena de bits con todos los datos
         for (i=18;i==0;i--)
         {  for (z=0;z==7;z++)
              { 
                 telemandobits[((i+1)*8)-(8-z)]=(int1) telemando1[i];
                 telemando1[i]>>=1; // x>>=y, is the same as x=x>>y
              }
         }


But in "telemandobits" there are no correct values.
Any idea??

Thanks
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 2:40 am     Reply with quote

Yes your "for loop" never executes.

It set i = 0, then test if i == 152, and because it is not equal to 152 it exits the loop.

You have to test for i <= 152 and while this evaluates to TRUE, the for loop will execute. Sorry I am bad at explaining anything, but hope this helps.

Regards
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 2:53 am     Reply with quote

OK Alan, you are a good teacher. Smile

regards
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 3:05 am     Reply with quote

Now the code is

Code:
     for (i=0;i<=151;i++)
     {
         telemandobits[i]=0;
     }

         telemando1[18]=8;
         telemando1[17]=7;
         telemando1[16]=6;
         telemando1[15]=5;
         telemando1[14]=4;
         telemando1[13]=12;
         telemando1[12]=0;
         telemando1[11]=1;
         telemando1[10]=17;
         telemando1[9]=255;
         telemando1[8]=255;
         telemando1[7]=255;
         telemando1[6]=255;
         telemando1[5]=255;
         telemando1[4]=255;
         telemando1[3]=255;
         telemando1[2]=255;
         telemando1[1]=0;
         telemando1[0]=0;

         //Aquí creo una cadena de bits con todos los datos
         for (i=0;i<=18;i++)
         {  for (z=0;z<=7;z++)
            { 
               telemandobits[((i+1)*8)-(8-z)]=(int1) telemando1[i];
               telemando1[i]>>=1; // x>>=y, is the same as x=x>>y
            }
         }
   


But in "telemandobits" array i have 1,0,1,0,1,0,1,0,1,0,...
Ttelmah



Joined: 11 Mar 2010
Posts: 19548

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 3:34 am     Reply with quote

>>=1, multiplies the value by 2. To get the next bit into the low bit you need <<=1. Explicitly test the low bit (=telemando1[i] & 1;). No cast needed.

However this is all basically not needed. You can create a union between the bits and the bytes, and the bits will all be there, with no copying needed. Instantaneous, versus a lot of code....
Code:

   union {
      int telemando1[19];
      int1 telemandobits[152];
  } val;
   
  val.telemando1[18]=8;
  val.telemando1[17]=7;
  val.telemando1[16]=6;
  val.telemando1[15]=5;
  val.telemando1[14]=4;
  val.telemando1[13]=12;
  val.telemando1[12]=0;
  val.telemando1[11]=1;
  val.telemando1[10]=17;
  val.telemando1[9]=255;
  val.telemando1[8]=255;
  val.telemando1[7]=255;
  val.telemando1[6]=255;
  val.telemando1[5]=255;
  val.telemando1[4]=255;
  val.telemando1[3]=255;
  val.telemando1[2]=255;
  val.telemando1[1]=0;
  val.telemando1[0]=0;


Then val.telemandobits[0] to [152], will contain the bits.

Best Wishes
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 3:47 am     Reply with quote

Many thanks.

Never used "union", i go to use it.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 3:48 am     Reply with quote

Hi Tthelmah, haven't you got it mixed. shift right >> divide and shift left << multiplies.

Regards
Alan
karlosguay



Joined: 12 Jun 2013
Posts: 20

View user's profile Send private message

PostPosted: Wed Sep 11, 2013 4:15 am     Reply with quote

Using "union" runs ok.
then the code is better.

Code:
   union {
      int telemando1[19];
      int1 telemandobits[152];
         } val;
 
//     for (i=0;i<=151;i++)
//     {
//         telemandobits[i]=0;
//     }

         val.telemando1[18]=8;
         val.telemando1[17]=7;
         val.telemando1[16]=6;
         val.telemando1[15]=5;
         val.telemando1[14]=4;
         val.telemando1[13]=12;
         val.telemando1[12]=0;
         val.telemando1[11]=1;
         val.telemando1[10]=17;
         val.telemando1[9]=255;
         val.telemando1[8]=255;
         val.telemando1[7]=255;
         val.telemando1[6]=255;
         val.telemando1[5]=255;
         val.telemando1[4]=255;
         val.telemando1[3]=255;
         val.telemando1[2]=255;
         val.telemando1[1]=0;
         val.telemando1[0]=0;

         //Aquí creo una cadena de bits con todos los datos
//         for (i=0;i<=18;i++)
//         {  for (z=0;z<=7;z++)
//            { 
//               telemandobits[((i+1)*8)-(8-z)]=(int1) telemando1[i];
//               telemando1[i]>>=1; // x>>=y, is the same as x=x>>y
//            }
//         }
         


Many thanks to all
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