Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

(CString) BOOL

Posted on 2001-07-27
13
Medium Priority
?
2,876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Accepted Solution

by:
nietod earned 80 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

636 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