how to convert NSinteger to String [duplicate] - iphone

This question already has answers here:
How to convert int to NSString?
(4 answers)
Closed 8 years ago.
I want to convet int to string in objective c how to do that.
my code.
for (i=0; i<=200; i=i+10) {
// here i want to convet the value of i into string how to do this
}
Thanks in Advance.

Try this:
NSMutableString *myWord = [[NSMutableString alloc] init];
for (int i=0; i<=200; i=i+10) {
[myWord appendString:[NSString stringWithFormat:#"%d", i]];
//...
}
//do something with myWord...
[myWord release];
NSInteger is simply a typedef to the int or long data types on 32/64-bit systems.

NSInteger n = 13;
NSString string = #(n).stringValue;
Reference see Objective-C literals - literals remove lots of ugly boilerplate code cluttering up your codebase: http://clang.llvm.org/docs/ObjectiveCLiterals.html

You may want to declare myWord out of the loop as NSMutableString.

Related

how to convert Nsstring value into Hexdecimal in ios [duplicate]

This question already has answers here:
How to convert an NSString to hex values
(8 answers)
Closed 9 years ago.
Hello i'm beginner in iOS In one of my activity I have NSString and want to convert into Hexdecimal format
NSString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
I am using this I then get result 0x772589fb51d3 and I want to extract this no 772589fb51d3
When I am using these line for this ....
NSString *str1=[NSString stringWithFormat:#"%llu",decimalRepresentation];
NSLog(#"str %#",str1);
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#"0x"];
str1 = [[str1 componentsSeparatedByCharactersInSet:doNotWant]componentsJoinedByString:#""];
NSLog(#"%#", str1); // => foobarbazfoo
but this line again convert this hexdecimal value into string I don't extract this value
0x772589fb51d3 as 772589fb51d3 .please help me....
Thanks In advance
I have tried like this:-
NsString *str= 131003112444371;
long long decimalRepresentation = 131003112444371;
NSLog(#"date %llx",decimalRepresentation);
NSLog(#"Hex value of char is 0x%02llx",decimalRepresentation);
//just replaced llu to llx
NSString *str1=[NSString stringWithFormat:#"%llx",decimalRepresentation];
NSLog(#"str %#",str1);
And the output i got here is 772589fb51d3
One Possible solution of this question:
+(NSString*)hexFromStr:(NSString*)str
{
NSData* nsData = [str dataUsingEncoding:NSUTF8StringEncoding];
const char* data = [nsData bytes];
NSUInteger len = nsData.length;
NSMutableString* hex = [NSMutableString string];
for(int i = 0; i < len; ++i)[hex appendFormat:#"%02X", data[i]];
return hex;
}

Convert string from special character [duplicate]

This question already has answers here:
Converting escaped UTF8 characters back to their original form
(2 answers)
Closed 9 years ago.
I am getting info using social framework and getting facebook info, when i get username in another language i.e Denis then it give me like "\U00e6\U00f8\U00e5", how to convert this string to readable string i.e Denis?
I think these chracters are Unicode chracters, you can try something like this to convert them to UTF-8
http://effbot.org/zone/unicode-convert.htm
Try with following code
Your string that you got,
NSString *myStr = #".....;
And use this code,
const wchar_t *myResulr = (const wchar_t*)[myStr cStringUsingEncoding:NSUTF16StringEncoding];
NSLog(#"\n str2 = %S",myResulr);
This also may be Helpful in your case.
Try this
NSString *str = [NSString stringWithUTF8String:"\u00e6\u00f8\u00e5"];
NSLog(#"%#", str);
I found this usefull solution for my problem. Check this link
OR
int main (int argc, const char * argv[])
{
#autoreleasepool {
NSString *name2escaped = #"\U00e6\U00f8\U00e5";
NSString *name2 = [NSString
stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"name2 = %#", name2);
}
return 0;
}
Try:
NSString *str = #"\U00e6\U00f8\U00e5";
NSString *string = [NSString stringWithCString:[str UTF8String]
encoding:NSUTF8StringEncoding];
NSLog(#"string: %#", string);

How to concatenate all values of array in Xcode [duplicate]

This question already has answers here:
Join an Array in Objective-C
(3 answers)
Closed 9 years ago.
I am new in iOS and i am not able to find solution of my question . So please tell me how can i concatenate all values of array in iOS
Answer : -componentsJoinedByString: .
It will join the components in the array by the specified string and return a string representation of the array.
Use : NSString *finalStr = [yourArray componentsJoinedByString:#" "];
GoodLuck !!!
if you are using string values in NSArray then it is quite simple.
You can just run for loop and use a NSMutableString to append the data.
NSArray *_array;//Your Initialized Array
NSMutableString *_string =[[NSMutableString alloc]init];
for(int i= 0 ;i<[_array count];i++)
{
[_string appendFormat:#"%#",[_array objectAtIndex:i]];
}
NSLog(#"_string = %#",_string);
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<[array count];i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
NSLog(#"mystirng=%#", theString;);

NSMutableString append is not working [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: How do you append a string to an NSMutableString?
I have a NSMutableArray. I want to extract all elements of this NSMutable array and append it in to a string. My following code is not working. String is not append to the variable. Also I am using automatic reference counting feature in this project.
NSMutableString *theString = [NSMutableString string];
for(int i=0; i<[self.incomingMessages count]; i++) {
id object = [self.incomingMessages objectAtIndex: i];
[theString appendString:(NSString*)object];
}
NSLog(#"the appended string: %#", theString);
[self.incomingMessages removeAllObjects];
How can I append the stored strings into the theString variable. Whats is the mistake I have done?
Do this
NSMutableString *theString = [[NSMutableString alloc]init ];
Try to print and see if your object is string
Maybe you need to initialize your theString like this?
NSMutableString *theString = [NSMutableString stringWithString:#""];

How to Concatenate String in Objective-C (iPhone)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I concatenate strings in Objective-C?
Firstly, the platform is iPhone and label.text changes the label displayed. Consider this scenario:
I've an array of integers. And I want to display it on the screen.
Here's my take on it:
-(IBAction) updateText: (id)sender {
int a[2];
a[0]=1;
a[1]=2;
a[2]=3;
for (int i=0; i<=10;i++)
label.text = [NSString stringByAppendingString: [NSString stringWithFormat: #"%i", a[i]]];
}
As you can probably see, I'm pretty confused. Pls pls help me out :(
Try this:
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendString:[NSString stringWithFormat:#"%i ",i]];
}
label.text = theString;
Since you're using a loop, do be somewhat careful with both Tom and Benjie's solutions. They each create an extra autoreleased object per iteration. For a small loop, that's fine, but if the size of the loop is unbounded or if the strings are large, this can lead to a very large memory spike and performance hit. Particularly on iPhone, this is exactly the kind of loop that can lead to surprising memory problems due to short-lived memory spikes.
The following solution has a smaller memory footprint (it's also slightly faster and takes less typing). Note the call to -appendFormat: rather than -appendString. This avoids creating a second string that will be thrown away. Remember that the final string has an extra space at the end that you may want to get rid of. You can fix that by either treating the first or last iteration differently, or by trimming the last space after the loop.
NSMutableString* theString = [NSMutableString string];
for (int i=0; i<=10;i++){
[theString appendFormat:#"%i ",i];
}
label.text = theString;
Don't forget [NSArray componentsJoinedByString:]. In this case you don't have an NSArray, but in the common cases where you do, this is probably the best way to get what you're looking for.
//NSArray *chunks
string = [chunks componentsJoinedByString: #","];
Another method without using NSMutableString:
NSString* theString = #"";
for (int i=0; i<=10;i++){
theString = [theString stringByAppendingFormat:#"%i ",i];
}
label.text = theString;
Here's a full implementation (correcting your ranges):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSString *str = #"";
for (int i=0; i<3;i++)
str = [str stringByAppendingFormat:#"%i ",i];
label.text = str;
}
You could also do it like this (e.g. if you wanted a comma separated list):
-(IBAction) updateText: (id)sender {
int a[3];
a[0]=1;
a[1]=2;
a[2]=3;
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];
for (int i=0; i<3;i++)
[arr addObject:[NSString stringWithFormat:#"%i",i]];
label.text = [arr componentsJoinedByString:#", "];
}