I have a string of 8 bits and I want to convert it into 1 byte. I am not sure why my function is not working properly. I have 8 bits stored into an array of 8 unsigned chars. This is my method so far:
unsigned int bitsToBytes(unsigned char *bits)
{
unsigned int sum = 0;
for(int i = 8; i >= 0; i--)
{
sum += bits[i];
sum<<=1;
}
return sum;
}
int main()
{
unsigned char bits[8]='01010111';
unsigned int byt;
byt = bitsToBytes(bits);
cout << byt; //doesn't give me the right result
}
Could anyone help to get correct result? Thanks alot!
Aucun commentaire:
Enregistrer un commentaire