Solved

(CString) BOOL

Posted on 2001-07-27
13
2,681 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

20 Experts available now in Live!

Get 1:1 Help Now