مشاهدة النسخة كاملة : Convert 8 bits to byte


م. عبدالله العنزي
18-04-2007, 10:40 AM
بسم الله الرحمن الرحيم

كل التحايا لكم اخواني اخواتي الكرام

واسمحولي بمصافحة قلوبكم العطره في اول مشاركاتي

وهو طلب اسأل الله أن أجده لديكم


اواجه مشكله بتحويل .. اسمحولي بالانجليزي

when i want to convert 8 bits to one byte

I have this code

char x

bool bits[8] and each element from 0 to 7 have value zero or one

I want inject those bits in char x



أرجو المساعده..

ذلك ولكم كل التقدير والاحترام

أبو أمير
21-04-2007, 11:35 AM
we can use the basic transform operation from bits to byte
int bits[8] ={1,1,1,1,1,1,1,1};// try bool
int x = 1*bits[0]+2*bits[1]+4*bits[2]+8*bits[3]+pow(2,4)*bits[4]+
pow(2,5)*bits[5]+pow(2,6)*bits[6]+pow(2,7)*bits[7];// x is in byte

ايليا
23-04-2007, 11:53 PM
salam
you can convert array [8] bits to byte by the following code :

#include <math.h>
unsigned char convert(int bit[]) //function return one byte
**
unsigend char s=0;
for(int i=0;i<8;i++)
s+=bit[i]*pow(2,i); //pow is math function found in header file math.h
return s;
}