# binary to ascii

• 04-19-2002
ygfperson
binary to ascii
Code:

```string to_string() {     string temp;     unsigned char x=0;     for (int i=0;i<bitt.size();i++) {       if (i%8==0 && i!=0) { temp+=x; x=0; }       if (bitt[i])             x |= (unsigned char)pow(2,i);       if (bitt[i]==0)             x &= ~(unsigned char)pow(2,i);     }     temp+=x;     return temp;   }```
bitt is a vector array of bools. i know they're non-standard (could anyone tell me how non-standard?) but it makes my life easier. essentially, there's a string of ones and zeros:
0001001111011 etc
i want to parse them eight bits at a time, and combine them into one unsigned char to be later written to a string, and then to a file. later on i'm going to add three bits to the end to show how much of the packet is left at the end. in other words:
11111111 01010101 10111110 10: 010 is 2, meaning 3 8 bit packets (aka, bytes) and 2 bits left over. the left over bits will have zeros added to pad it up to a full byte.
11111111 01010101 10111110 10000000
for some reason the above code doesn't work. can anyone spot problems? thanks for your time :D
• 04-20-2002
swoopy
Try:
Code:

```string to_string() {     string temp;     unsigned char x=0;     for (int i=0;i<bitt.size();i++) {       if (i%8==0 && i!=0) { temp+=x; x=0; }       if (bitt[i])             x |= (unsigned char)pow(2,i%8);     }     temp+=x;     return temp;   }```
Or you could use a mask:
Code:

```string to_string() {     string temp;     unsigned char x=0;     unsigned char mask = 1;     for (int i=0;i<bitt.size();i++) {       if (i%8==0 && i!=0) { temp+=x; x=0; mask=1;}       if (bitt[i])             x |= mask;       mask <<= 1;     }     temp+=x;     return temp;   }```
• 04-20-2002
ygfperson
oh, i see. thanks :)