Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to convert BOOL value into INT?

Everytime when I tried to convert BOOL value into int. INT value is showing -8 for True and 0 for False. Ideally it should return 1 for true.

I have tried all possible ways of conversion like

int val=Info.isattachementDownloadable;
int val=[[NSString stringWithFormat:@"%d", Info.isattachementDownloadable] intValue];
int val=(int)Info.isattachementDownloadable;

where Info.isattachementDownloadable return BOOL

In all ways its showing -8 for TRUE.

Any suggestion ?

like image 794
Tariq Avatar asked Jul 21 '11 11:07

Tariq


People also ask

Can we convert boolean to int in C?

Master C and Embedded C Programming- Learn as you go Bool is a datatype in C++, and we can use true or false keyword for it. If we want to convert bool to int, we can use typecasting. Always true value will be 1, and false value will be 0.

How do you convert boolean to int in Python?

Use int for casting a Boolean value in Python. Using int() method will convert Boolean to Int, 1 for True and 0 for False.

Can you cast a bool to an int C#?

First, you cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. It is the same rule that mandates you cannot test an integer in an if statement.


1 Answers

Maybe that will help(thought i don't know why it may be needed in the first place)

NSInteger i = @(YES).integerValue;

Hope that it helps you.

like image 165
Ariel Avatar answered Oct 10 '22 12:10

Ariel