converting bool[8] to char

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

converting bool[8] to char

 
0
  #1
Nov 22nd, 2008
hey..I need to convert a 8 value bool array to char.How can I do this?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: converting bool[8] to char

 
0
  #2
Nov 22nd, 2008
You can use the bool array as a "bit-position" array for the representation of a char.

  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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 181
Reputation: Paul.Esson is an unknown quantity at this point 
Solved Threads: 10
Paul.Esson's Avatar
Paul.Esson Paul.Esson is offline Offline
Junior Poster

Re: converting bool[8] to char

 
0
  #3
Nov 22nd, 2008
I had
  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'.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: converting bool[8] to char

 
0
  #4
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,431
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: converting bool[8] to char

 
0
  #5
Nov 22nd, 2008
How about this, rather unusual method
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 62
Reputation: AutoC is an unknown quantity at this point 
Solved Threads: 0
AutoC AutoC is offline Offline
Junior Poster in Training

Re: converting bool[8] to char

 
0
  #6
Nov 22nd, 2008
Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC