012346
012346

Reputation: 199

How to Convert float to Char * in Objective c

I am trying to convert to float to int

float flot=2.1456;
char*  c [] = {(char *)flot};
ObjLen = sizeof(c)/sizeof(*c);
NSLog(@"ObjLen %d",ObjLen);  

but it is showing error Operand of type 'float' cannot be cast to a pointer type

and Converting Char* to back again to float

Upvotes: 0

Views: 1651

Answers (2)

tikhop
tikhop

Reputation: 2087

I think better way is using sprintf or snprintf methods from stdio.h. sprintf/snprintf 2x faster than @Prince approach.

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Firstly convert float to NSString

float flot=2.1456;
NSString *str = [NSString stringWithFormat@"%f",flot];

Use -[NSString UTF8String]: to convert to char *

const char *c = [str UTF8String]

Upvotes: 1

Related Questions