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 :)
Related
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.
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
}
just like my question, How can i convert decimal into octal in objective c?
can somebody help me? it's make me dizy
You have to initialize a new NSString object using the right format specifier.
Like :
int i = 9;
NSString *octalString = [NSString stringWithFormat:#"%o", i]; // %O works too.
This will create an autoreleased NSString object containing octal string representation of i. In case you start from a NSString
containing a decimal number representation, you should first retrieve the number using :
int i = [myDecimalNumberAsAString intValue];
Here is a link to the format specifiers reference.
Hope this helps.
Since Objective C is a superset of C you can just use C functions such as sprintf, e.g.
char s[32];
int n = 42;
sprintf(s, "%o", n);
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
I want to get the double value from "1.04E-4" NSString, but I didn't manage yet how to do it.
I tried the following:
1.
NSString* str = #"1.0E-4";
double value = [str doubleValue]; //returns 0.0001
2.
NSString* str = #"1.0E-4";
double value;
NSScanner* scanner = [NSScanner scannerWithString:str];
[scanner scanDouble:&value];
//0.0001 again
Instead of value = 1.0E-4, I get 0.0001
Could someone help me with this?
Appreciate,
Alex.
For the record, 1.0E-4 = 0.0001.
In Java, if I use Double.parseDouble("1.0E-4") I get 1.0E-4, but in objc I don't know how to do this.I encounter some problems translating some Java code into objc, and this is one of the differences that I've spot since now.