Solved

(CString) BOOL

Posted on 2001-07-27
13
2,699 Views
Last Modified: 2009-12-16
Hi

Is there an esy way to convert a BOOL to a string, like "1" = TRUE, "0" = FALSE

CB.
0
Comment
Question by:win32
  • 5
  • 2
  • 2
  • +4
13 Comments
 
LVL 2

Expert Comment

by:smitty1276
ID: 6325987
char boolString[2];
bool boolVal = whatever;

if( boolVal == true )
  strcpy( boolString, "1" );
else
  strcpy( boolString, "2" );


0
 
LVL 1

Expert Comment

by:ccaprar
ID: 6326000
unless you use .NET ( C# ) or Java, in C++ there is no such API or STD function. Why don't you define a little helper class, that contains the values and their string representations, and have methods as BooleanValue ( from CString, from char*, etc, ) and ToString (from bool). Take a look at Java's String and Boolean classes for some good examples
0
 
LVL 1

Expert Comment

by:Hoegje
ID: 6326099
string boolToString(bool toConv){
  if (toConv == true){
    string tr = "True";
    return tr;
  }
    string fa = "False";
    return fa;
}

I guess this should work...
0
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 6326341
>> n C++ there is no such API or STD function.
I dissagree--slightly--well, very slightly.

When you ouput a boolean value to a stream using the formatted I/O  (insertion operator (operator <<)) false is oupyutted as 0 and true as 1.   so for example

cout << false;

outputs "0".

You can always use a string stream class to convert formatted I/O to a string like

stringstream S;
bool f = false;
bool t = true;

S << "this is false: " << f << " this is true: " << t;

string AString = S.str();

Noe AString contains a string that says "this is false: 0 this is true: 1".
0
 
LVL 22

Expert Comment

by:nietod
ID: 6326351
Opps did anyone notice this is for a "CString"  ie. MFC?

You could use CString's Format() member for this.  You could take advantage of the fact that when bool is cast to an int false becomes 0 and tue becomes 1, like

CString S;
bool f = false;

S.Format("this is false: %i", (int) f);


HOWEVER, variable argument procedures like Format() are a little risky to use.  I woudl avoid them wherever possible.  So I woudl actually do something more like what smitty suggested.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6326723
BOOL b;
CString str = b?"TRUE":"FALSE";

or

bool b;
std::string str = b?"TRUE":"FALSE";
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:makerp
ID: 6326737
>>HOWEVER, variable argument procedures like Format() are
a little risky to use

why neitod
0
 
LVL 22

Expert Comment

by:nietod
ID: 6326774
VA functions provide no-type safety.   Simple mistakes that would ordinarly be caught by the compiler are not detected and can result in crashes or erratic behavior.
0
 
LVL 10

Expert Comment

by:makerp
ID: 6326828
is there any safe alternative then ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6327231
Yes.  STL doesn't use any VA functions.  In fact, the STL stream classes were developed as a way to avoid VA functions like printf()  (Long before templates were added to the langauge and long before STL was called STL).
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6327552
What about something as simple as
   return (char)(false + 48); // = '0'
   return (char)(true + 48); // = '0'

#include <afx.h>

char     GetAnswer(bool blnAnswer)
{
     return ((char)(blnAnswer + 48));
}

void main(void)
{
     printf("%c\n"     , GetAnswer(true));
}



0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6327597
...that should have read...

  return (char)(false + 48); // = '0'
  return (char)(true + 48); // = '1'
0
 
LVL 22

Expert Comment

by:nietod
ID: 6327663
Why assume that '0" is 48 dec?

Yoiu could do

char     GetAnswer(bool blnAnswer)
{
    return (char)((int)blnAnswer + '0');
}

But seeign as this is askign for a CString, not a char, I don't see how this is that useful.  

bool theBool = false;
CString S = theBool?'1';'0';

would be more useful.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 119
Task manager indicates my c++ program memory consumption is growing? 12 98
I could not build boost code, 10 78
max float value 3 25
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now