How to convert a string to float? [duplicate] - objective-c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert string to float in Objective-C
I'd like to convert a string to a float.
/* dict is an NSDictionary to load Preferences */
NSString *str = [dict objectForKey:#"key"];
This is where I got. Now I'd like to convert the string value (in this case #"32.0f") in a float, where it could be processed by my application. How can I do this?

CGFloat strFloat = (CGFloat)[str floatValue];

Just pass the message floatValue to the NSString object. NSString::floatValue

Related

Hexadecimal NSString to int Objective C [duplicate]

This question already has an answer here:
How to convert hexadecimal string to decimal?
(1 answer)
Closed 5 years ago.
I am currently struggling to figure out how to convert a Hexadecimal NSString into a calculated int via Objective C.
I am trying to have an Objective C Function that would get inputed with an NSString eg. Like this:
NSString *hexString = #"0x0f";
and return the decimal value (as an int):
int hexValue = 15; (converted)
eg. use this website to learn how to get the decimal of Hexadecimal:
http://www.binaryhexconverter.com/hex-to-decimal-converter
If someone could show me how to do this this would be great thanks :D
(in Objective C) and please try to make the code a small as possible :)
Don't Worry I figured it out. If anyone else has this question then you can use my answer:
NSString *hexString = #"0x0f"; // hexString is inputted
unsigned value = 0;
NSScanner *scanner = [NSScanner scannerWithString:hexString];
[scanner scanHexInt:&value];
int decimalValue = (int)value; // decimalValue is outputted
Hope this helps for anyone who needs this :)

Convert NSUInteger to string with ARC [duplicate]

This question already has answers here:
NSUInteger should not be used in format strings?
(4 answers)
Closed 8 years ago.
I'm trying to cast a NSUInteger to a string so I can print a message. From searching, it seems like I need to use stringWithFormat, but I am getting an error that an implicit cast not allowed with ARC.
Here's the line in question:
NSString *text = [[NSString stringWithFormat: (#"%li", NSUInteger)];
I've tried changing the format specifier to %lu with no help.
Thanks.
You probably have a variable of type NSUInteger, something like
NSUInteger myNumber;
Then you can convert it to a string like this:
NSString *text = [NSString stringWithFormat:#"%li", myNumber];
A solution that I prefer now is this:
NSString *text = [NSString stringWithFormat:#"%#", #(myNumber)];
This helps avoid compile warnings about incorrect number formatting codes (after a long time I still get confused in them).

Floating Point things work.....Most of the time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Correcting floating point numbers
float randNum = arc4random()%(100)-1;
I read somewhere that this will give me random numbers between 1-100. Or something close to that.
This line seems to work all the time, but I convert this number to an NSString for storage/printing to text, and then convert it back to a float for ordering and other calculations. But when I get that float back sometimes it looks something like gibberish; like this in the variable view:
9 float 9.75303731e-41
Why would converting to an NSString and back to a float ruin the float value I'm using? (e.g. What could I have screwed up? Or should I be using CGFloat instead?)
I think this is all the pertinent code:
NSMutableArray *stringArray = [[NSMutableArray alloc] init];
floatArray[30];
// put three random floats into an NSMutableArray of NSStrings
for(int i = 0; i < 3; i++)
{
float randNum = arc4random()%(100)-1;
NSString *randString = [NSString stringWithFormat:#"%.3f", randNum];
[stringArray addObject:randString];
}
// convert NSStrings back to float
for(NSString *string in stringArray)
{
float temp = [string floatValue];
floatArray[iterator] = temp;
}
Thanks in advance for any help/advice.
EDIT: When I step through the code it looks like the float value looks sane until the line "float temp = [string floatValue]", which is where the value seems to be garbaged.
Why are you using float when the result from arc4random() is a uint32_t? Switching to integer types would almost certainly get around all this, as I suspect the problem is because of the conversion to string form allowing only 3 significant digits. What happens if you use %.15f as your format?
Sidenote: use arc4random_uniform() - it's simpler and guaranteed to be a uniformly random distribution within that range.

Compare char array with string in iOS program [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to compare char* and NSString?
If I have:
char XYZ[256]="";
how can I compare this char array with another string (e.g. "testing") in an iOS Objective-C program?
Use strcmp
char XYZ[256] = "";
char *string = "some other string";
int order = strcmp(XYZ, string);
RETURN VALUES
The strcmp() and strncmp() functions return an integer greater than, equal to, or less than 0, according as the string s1 is greater than, equal to, or less than the string s2. The comparison is done using unsigned characters, so that \200' is greater than\0'.
You can also convert them up to NSString, this makes a lot overhead, but brings your string to Objective-C object:
char XYZ[256] = "";
NSString *s = [[NSString alloc] initWithBytes:XYZ length:strlen(XYZ) encoding:[NSString defaultCStringEncoding]];
NSString *testing = #"testing";
if ([testing compare:s] == NSOrderedSame) {
NSLog(#"They are hte same!");
}
Note that strcmp is A LOT faster!
Just because it is iOS doesnt mean that you cannot "#include" string.h and use "strcmp" (now as stated above).
The alternative would be to create a new NSString and compare it using a comperable iOS Objective-C call:
NSString myString = [NSString stringWithCString:XYZ encodingNSASCIIStringEncoding];
if(YES == [myString isEqualToString:#"testing"]){
// Perform Code if the strings are equal
}else{
// Perform Code if the strings are NOT equal
}

EXC_BAD_ACCESS strange Hex to String conversion [duplicate]

This question already has answers here:
Hex in a stringWithFormat BAD ACCESS
(2 answers)
Closed 8 years ago.
I'm stuck.
I give you the exact code which I use, not a sample:
So, I make two strings like this:
DestChoice = [NSString stringWithFormat:#"%lX", [DestField integerValue]];
SourceChoice = [NSString stringWithFormat:#"%lX", [SourceField integerValue]];
So the user write the Source/Dest in Decimal value but they are stocked as hex in a string, and then I have a button which reformat the source & dest like this:
NSString * CommandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", DestChoice, SourceChoice];
My code is working, BUT, strangly some values makes a EXC_BAD_ACCESS at the CommandReadyToSend part...
Example: If I write 1 in my text field => I receive 1 (hex value) as result in DestChoice
If I write 10 in my text field => I receive A in DestChoice
BUT If I write 31 in the text field, I'm supposed to get 1F, but I get a EXC_BAD_ACCESS...
Same thing with 29 (dec value)
It seems that certains hex value can't be reformatted to string, I don't understand...
Seems to be working for me, the cause must be elsewhere.
NSString *destChoice = [NSString stringWithFormat:#"%lX", 1];
NSString *sourceChoice = [NSString stringWithFormat:#"%lX", 31];
NSString *commandReadyToSend = [NSString stringWithFormat:#"X:0/%#,%#\r", destChoice, sourceChoice];
NSLog(#"%#", commandReadyToSend);
2012-05-10 13:56:29.092 test[9383:707] X:0/1,1F
-
btw; If DestField is a UITextField then you should be using [DestField.text integerValue]; - this is probability your problem.