| | |
converting bool[8] to char
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
You can use the bool array as a "bit-position" array for the representation of a char.
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
c++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; int main(){ bool bits[] = {0, 1, 0, 0, 0, 0, 0, 1}; char c = 0; for(int i = 0; i < 8; i++) c += (bits[i] >> i); // 0 or 1 times 2 to the ith power cout << c << endl; // should print out A cin.get(); return 0; }
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.
I had
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)
char boolsToChar(bool* bools){ char c = 0; for( int i = 0; i < 8; i++ ) if( bools[i] ) c += pow(2,i); return c; }
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)
bool bits[] = {1, 0, 0, 0, 0, 0, 1, 0}; char c = 0; for(int i = 0; i < 8; i++) c += (bits[i] << i); // 0 or 1 times 2 to the ith power cout << c << endl; // should print out A cin.get();
Last edited by Paul.Esson; Nov 22nd, 2008 at 7:18 am.
•
•
Join Date: Mar 2008
Posts: 1,431
Reputation:
Solved Threads: 118
How about this, rather unusual method 
Theres practically no maths involved

C++ Syntax (Toggle Plain Text)
#include <iostream> struct octet { union { char val; struct { unsigned h : 1; unsigned g : 1; unsigned f : 1; unsigned e : 1; unsigned d : 1; unsigned c : 1; unsigned b : 1; unsigned a : 1; }; }; }; int main() { bool bits[] = {0, 1, 1, 0, 0, 0, 0, 1}; octet o; o.a = bits[0]; o.b = bits[1]; o.c = bits[2]; o.d = bits[3]; o.e = bits[4]; o.f = bits[5]; o.g = bits[6]; o.h = bits[7]; std::cout << o.val; // 01100001 = 'a' std::cin.ignore(); }
![]() |
Similar Threads
- Simple ASP.Net Login Page using C# (C#)
- Converting From Pointer/Address to String for Validation (C++)
- need solution to converting CHAR to WORD (C++)
- error C2664 (C++)
- Searching linked list (C)
- Linked list search problems (Computer Science)
- Help with Class, stuck. (C++)
- Error Linking KeyLogger.exe (C++)
Other Threads in the C++ Forum
- Previous Thread: ordering arrays
- Next Thread: Parsing error
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






