Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I want convert bool array with length 8 to char. But it doesn't work. What are the reasons?

Upd. Literally I want somewhat that std::bitset::to_ulong function does.

What I have tried:


void print(bool *b) {
unsigned char *c = (unsigned char*)(b);
std::cout << *c;
}
Posted
Updated 22-Nov-17 2:13am
v3

The size of bool values depends on the implementation (the compiler). It might be a single byte or more. Common is the size of an int. You can check it by printing sizeof(bool).

Casting a pointer for different types will only work if both types have the same size. But even then you should know what you do (know how to interpret and use the casted data).

You try to print out a single character (use the * dereference operator). The value of that character depends here again on the implementation but in most cases it should be zero or one which are both not printable characters.

If you want to print bool values you can use I/O manipulators to define what to be printed (false / true or 0 / 1):
C++
cout << std::boolalpha << booleanValue << '\n';
cout << std::noboolalpha << booleanValue << '\n';

If you need a real conversion, you have to create an output array, iterate over the boolean input array, and assign corresponding values:
C++
unsigned char ucOut[8];
char strOut[9];
for (int i = 0, i < 8; i++)
{
    ucOut[i] = b[i] ? 1 : 0;
    // or
    //ucOut[i] = static_cast<unsigned char>(b[i]);
    strOut[i] = b[i] ? '1' : '0';
}
strOut[8] = '\0';
std::cout << strOut;
Note that you can use casting inside the loop because that will cast a single value and not an array. But it can be only used for binary values and not for characters to be printed.

[EDIT]
Note also that I have used a static_cast. If you use that in your code for the pointers, the compiler will throw an error. Avoid the old C style casting with C++. There is also a C++ cast operator that can cast the pointers from your example. But when using such (reinterpret) it (hopefully) indicates that the writer of the code knows what he is doing there.
[/EDIT]
 
Share this answer
 
v2
Comments
Member 13535370 22-Nov-17 8:31am    
Oh thank you for pointing out that bool takes 1 byte. So 1 bool literally equals 1 char. I forgot that.
Jochen Arndt 22-Nov-17 8:40am    
No! I did not wrote that. I wrote that it depends on the used compiler. It may be also the size of an int (4 / 8 bytes with 32-/64-bit builds)!

Thank you for accepting my solution.
You haven't to. Try
C++
#include <iostream>
using namespace std;

void print(bool *ba, size_t size)
{
  for ( size_t n = 0; n < size; ++n)
  {
    cout << ba[n];
    cout << ( n < size-1 ? ", " : "\n");
  }
}


int main()
{
  bool ba[] = { true, false, false };
  print(ba, sizeof(ba)/sizeof(ba[0]));
}
 
Share this answer
 
Comments
Member 13535370 22-Nov-17 8:11am    
How this should help me? This function simply prints bool array values.
CPallini 22-Nov-17 8:40am    
What do you need then? Could you please make an example (that is input and expected output)?
Member 13535370 22-Nov-17 8:45am    
I've got the right answer in solution 2.
I thought that sizeof(bool[8]) == sizeof(char). So I wanted to convert bool[8] to char.
CPallini 22-Nov-17 8:50am    
Nope. It is implementation defined. See, for instance
https://stackoverflow.com/questions/4897844/is-sizeofbool-defined
phil.o 22-Nov-17 8:50am    
Even if they had the same size in memory (which is not certain as Mr Arndt told you), false would be cast to char \0, which is the null character, and true would be cast to char \1, which is the SOH (Start Of Heading) character; both of them are not printable. You seem to confound the char value with its textual representation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900