I am trying to convert a nsstring with hex values into a float value.
NSString *hexString = @"3f9d70a4";
The float value should be = 1.230
.
Some ways I have tried to solve this are:
1.NSScanner
-(unsigned int)strfloatvalue:(NSString *)str
{
float outVal;
NSString *newStr = [NSString stringWithFormat:@"0x%@",str];
NSScanner* scanner = [NSScanner scannerWithString:newStr];
NSLog(@"string %@",newStr);
bool test = [scanner scanHexFloat:&outVal];
NSLog(@"scanner result %d = %a (or %f)",test,outVal,outVal);
return outVal;
}
results:
string 0x3f9d70a4
scanner result 1 = 0x1.fceb86p+29 (or 1067282624.000000)
2.casting pointers
NSNumber * xPtr = [NSNumber numberWithFloat:[(NSNumber *)@"3f9d70a4" floatValue]];
result:3.000000