943,587 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2940
  • C++ RSS
Nov 22nd, 2008
0

converting bool[8] to char

Expand Post »
hey..I need to convert a 8 value bool array to char.How can I do this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008
Nov 22nd, 2008
0

Re: converting bool[8] to char

You can use the bool array as a "bit-position" array for the representation of a char.

c++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3.  
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7.  
  8. int main(){
  9. bool bits[] = {0, 1, 0, 0, 0, 0, 0, 1};
  10. char c = 0;
  11. for(int i = 0; i < 8; i++)
  12. c += (bits[i] >> i); // 0 or 1 times 2 to the ith power
  13. cout << c << endl; // should print out A
  14. cin.get();
  15. return 0;
  16. }

Edit: I'm not near a compiler, so the bit-shifting operator may need to be switched but the logic is straightfoward.

Hopefully this helps! =)

-Alex
Last edited by Alex Edwards; Nov 22nd, 2008 at 6:36 am.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 22nd, 2008
0

Re: converting bool[8] to char

I had
c++ Syntax (Toggle Plain Text)
  1. char boolsToChar(bool* bools){
  2. char c = 0;
  3. for( int i = 0; i < 8; i++ )
  4. if( bools[i] )
  5. c += pow(2,i);
  6. return c;
  7. }

but clearly this is not as nice a solution as above, For multiple reasons.

One being that I get a warning for converting a double to a char and two I have a conditional statment that i clearly don't require

Anyhow the solution above compiled for me, but I needed to change it around a little to come up with 'A'.

c++ Syntax (Toggle Plain Text)
  1. bool bits[] = {1, 0, 0, 0, 0, 0, 1, 0};
  2. char c = 0;
  3. for(int i = 0; i < 8; i++)
  4. c += (bits[i] << i); // 0 or 1 times 2 to the ith power
  5. cout << c << endl; // should print out A
  6. cin.get();
Last edited by Paul.Esson; Nov 22nd, 2008 at 7:18 am.
Reputation Points: 21
Solved Threads: 10
Junior Poster
Paul.Esson is offline Offline
181 posts
since Feb 2005
Nov 22nd, 2008
0

Re: converting bool[8] to char

Hmm, try changing char to unsigned (if it exists @_@ )

-Alex

Edit: I am really tired #_#

I didn't realize I made the array back-asswards XD

XP
Last edited by Alex Edwards; Nov 22nd, 2008 at 7:37 am.
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Nov 22nd, 2008
0

Re: converting bool[8] to char

How about this, rather unusual method
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. struct octet {
  4. union {
  5. char val;
  6. struct {
  7. unsigned h : 1;
  8. unsigned g : 1;
  9. unsigned f : 1;
  10. unsigned e : 1;
  11. unsigned d : 1;
  12. unsigned c : 1;
  13. unsigned b : 1;
  14. unsigned a : 1;
  15. };
  16. };
  17. };
  18.  
  19. int main() {
  20. bool bits[] = {0, 1, 1, 0, 0, 0, 0, 1};
  21.  
  22. octet o;
  23. o.a = bits[0];
  24. o.b = bits[1];
  25. o.c = bits[2];
  26. o.d = bits[3];
  27. o.e = bits[4];
  28. o.f = bits[5];
  29. o.g = bits[6];
  30. o.h = bits[7];
  31.  
  32. std::cout << o.val; // 01100001 = 'a'
  33. std::cin.ignore();
  34. }
Theres practically no maths involved
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Nov 22nd, 2008
0

Re: converting bool[8] to char

Thanks a lot!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
AutoC is offline Offline
74 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: ordering arrays
Next Thread in C++ Forum Timeline: Parsing error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC