Solved

(CString) BOOL

Posted on 2001-07-27
13
2,758 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 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