c - unsigned char array of 8 bits to unsigned char -


i've created function turns unsigned char unsigned char array of size 8 (where each index contains either 0 or 1, making 8 bits of given char). here 100% working version:

unsigned char * uchartobitarray(unsigned char c) {    unsigned char * bits = malloc(8);     int i;    for(i=sizeof(unsigned char)*8; i; c>>=1)        bits[--i] = '0'+(c&1);     return bits ; } 

i need create function exact opposite of now. meaning, take , unsigned char array of size 8, , turn regular single unsigned char. effective way of doing so?

thanks help!

the function needlessly complex , obscure. suggest replacing this:

void uchartobitarray(char bits[8], uint8_t c) {    for(uint8_t i=0; i<8; i++)    {      if(c & (1<<i))      {        bits[7-i] = '1';      }      else      {        bits[7-i] = '0';      }    } } 

now convert back, go other way around. check bits[i] , set c |= (1<<i) if found '1'.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -