|
|
View previous topic :: View next topic |
Author |
Message |
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
Problem to convert array of bytes to array of bits |
Posted: Wed Sep 11, 2013 2:08 am |
|
|
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.
Alguna sugerencia??
Gracias |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Sep 11, 2013 2:11 am |
|
|
Format are as follow for for loop
Code: | for (i=0; i<= 151, i++) { |
Regards |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Sep 11, 2013 2:16 am |
|
|
Sorry Alan, I don´t understand |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Sep 11, 2013 2:33 am |
|
|
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
|
|
Posted: Wed Sep 11, 2013 2:40 am |
|
|
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
|
|
Posted: Wed Sep 11, 2013 2:53 am |
|
|
OK Alan, you are a good teacher.
regards |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Sep 11, 2013 3:05 am |
|
|
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: 19515
|
|
Posted: Wed Sep 11, 2013 3:34 am |
|
|
>>=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
|
|
Posted: Wed Sep 11, 2013 3:47 am |
|
|
Many thanks.
Never used "union", i go to use it. |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Sep 11, 2013 3:48 am |
|
|
Hi Tthelmah, haven't you got it mixed. shift right >> divide and shift left << multiplies.
Regards
Alan |
|
|
karlosguay
Joined: 12 Jun 2013 Posts: 20
|
|
Posted: Wed Sep 11, 2013 4:15 am |
|
|
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 |
|
|
|
|
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
|