Solved

(CString) BOOL

Posted on 2001-07-27
13
2,799 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
Technology Partners: 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

689 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