I have an iPad app which communicates with a webservice. There I can download an encrypted file. In a particular request I get a json with login credentials. Also in that json is a key which is used to encrypt the data.
The key looks like:
[0,44,215,1,215,88,94,150]
With the json framework I can put this key into an NSMutableArray. After that I use a AES256 code to decrypt the file. But that code needs a NSString as a key. So my question is: how can I decode that NSMutableArray into an NSString? I guess I first need to put it into an byte arary, and then put it into an NSString?
Who can help me with this one?
Thanks in advance!
Firstly, convert your array of numbers (I assume they're given as NSNumbers) into a C array using code similar to the first snippet in the accepted answer here. In other words, something similar to this:
// Test array for now -- this data will come from JSON response
NSArray* nsArray = [NSArray arrayWithObjects:[NSNumber numberWithChar:1],
[NSNumber numberWithChar:2],
nil];
char cArray[2];
// Fill C-array with ints
int count = [nsArray count];
for (int i = 0; i < count; ++i) {
cArray[i] = [[nsArray objectAtIndex:i] charValue];
}
Then create an NSString using the correct encoding:
NSString *encodedStr = [NSString stringWithCString:cArray encoding:NSUTF8StringEncoding];
Note: these are code sketches, they haven't been tested!
EDIT: changed from ints to chars.
If your array is the sequence of numbers, you can loop through it
//Assume you have created keyArray from your JSON
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
[string appendFormat:#"%#", id];
}
// if you need the comma's in the string
NSMutableString * keyString = [NSMutableString string];
for (id element in keyArray) {
[string appendFormat:#"%#,", id];
}
int length = [string length];
NSRange range = NSMakeRange(0, length-1);
string = [string substringWithRange:range];
Related
Using the Evernote API, I have an object which has an NSUInteger property called hash. For the specific object I'm looking at, this is equal to:
<f5b5444b 33e740b7 f9d49a3b ddb6a39c>
I want to convert this into an NSString. Doing this:
[NSString stringWithFormat:#"%d", noteResource.hash]
Gives me this:
530049088
How could I correctly convert the hash value to an NSString?
When you see something output as "<" 8 hex digits space .... ">", it's the result of logging a NSData object (NSLog(#"%#", myDataObject);). So I believe what you have is not an NSUInteger, but a NSData * object.
There is no built in method to convert between strings and data, you need to do it in code:
- (NSString *)dataToString:(NSData *)data
{
NSUInteger len = [data length];
NSMutableString *str = [NSMutableString stringWithCapacity:len*2];
const uint8_t *bptr = [data bytes];
while(len--) [str appendFormat:#"%02.2x", *bptr++];
return str;
}
If this works, you can write your own stringToData method reversing the above, if needed.
I am parsing rss using nsxmlparser and I would like to get some images from the rss...how can I do that?
so here is an example of the rss
<p><img scr = "IMGURL"></p>
how can I get the "IMGURL"
thanks,
TC
Here is a quick hack - only do this if you know the strings are not going to change position. Otherwise you risk crashing.
//for demo purposes lets say your <p><img scr = "IMGURL"></p> is a string named sourceString
//chop it up into an array like this and grab it from the arrays position
//create an array
NSArray *tempArray = [sourceString componentsSeparatedByString:#"\""];
//what this has done, it has create an array of objects from your source string separated by "
//so in your tempArray you will have two objects
// objectAtIndex 0 will be: <p><img scr = you wont need this so we can ignore it
// objectAtIndex 1 will be the string you want. it will be: IMGURL
// so now you can quickly create a string from it like this
NSString * imgURL = [[tempArray objectAtIndex:1]description];
Its a quick and dirty trick... but it works! So long as the data stays the same format. Your call!
You need to use a xml parser like XQuery. The query would be: /p/img#src
You can use a function such as:
NSString *stringForXQuery(NSXMLNode *node, NSString *query)
{
NSArray *results = [node objectsForXQuery:query constants:nil error:nil];
NSUInteger howManyResults = [results count];
if (howManyResults != 1) {
return nil;
}
return [[results objectAtIndex:0] stringValue];
}
And call it like this:
NSString *imgURL = stringForXQuery([yourxmldocument rootElement], #"/p/img#src");
I have an NSString of integer values. I need to add each one of the integers as a separate object in an NSMutableArray.
I tried characterAtIndex: but I keep getting errors…
P.s. I've solved over 30 problems thank's to stackoverflow's search, but didn't find any information on this problem.
NSMutableArray *results = [NSMutableArray array];
for (int i = 0; i < [string length]; i++)
{
NSString *substr = [string substringWithRange:NSMakeRange(i,1)];
[results addObject:[NSNumber numberWithInt:[substr intValue]];
}
NSLog(#" %# separated into: %#", string, results);
Consider looking at componentsSeparatedByString: or componentsSeparatedByCharactersInSet: for your purpose. Both methods are available on NSString.
If your integers are more than one digit or are space-separated, consider alternately using an NSScanner - you can read instances of NSInteger off the string one by one, wrap them in NSNumbers, and stick them in your array.
i have to create an 2images and 3 labels by using code (cgrectmake)and i am having X location, y location, width and height all are stored in arrays(which i have retrieved from the web services)how can i create the image and labels can any one help me
You can join the elements of an array together with the NSString componentsJoinedByString class method:
NSString myString = [myNSArray componentsJoinedByString:#"x"];
where x is the characters you'd like to appear between each array element.
Edited to add
So in your newly-added code if these are the label values:
lbl = #"zero"
lbl1 = #"one"
lbl2 = #"two"
and you want to join them together with a space character then if you did this:
NSString *temp = [labelArray componentsJoinedByString:#" "];
NSLog(#"temp = %#", temp);
then this is what would be logged:
zero one two
Edited to further add
If you are instead trying to join the label values together to make xml elements then you might do something like this:
NSString *joinedElements = [labelArray componentsJoinedByString:#"</label><label>"];
NSString *temp = [NSString stringWithFormat:#"<label>%#</label>", joinedElements];
NSLog(#"temp = %#", temp);
then this is what would be logged:
<label>zero</label><label>one</label><label>two</label>
may be this is usefull to you.
NSString *str;
str = [arrayName objectAtIndex:i(Index NO)];
OK by this easily you can access object from the array. any type of object u can fetch this way only reception object type are change in left side.
Best of Luck.
Most objects have a -description method which returns a string representation of the object:
- (NSString *)description;
For example:
NSArray *array = [NSArray arrayWithObjects:#"The", #"quick", #"brown", #"fox", nil];
NSLog(#"%#", array); // prints the contents of the array out to the console.
NSString *arrayDescription = [array description]; // a string
It would help to know what you want to do with the string (how will you use the string). Also, what kind of objects do you have in the array?
In that case, Matthew's answer is one possibility. Another might be to use an NSMutableString and append the individual items, if you need control over how the string is created:
NSMutableString *string = [NSMutableString string];
if ([array count] >= 3) {
[string appendString:[array objectAtIndex:0]];
[string appendFormat:#"blah some filler text %#", [array objectAtIndex:1]];
[string appendString:[array objectAtIndex:2]];
}
I am writing some code to allow users to answer multiple choice questions. So I have an NSArray of values [#"value1", #"value2", ..]
I want to display them as:
A) value1
B) value2
The code I have is
for(int i = i; i < [values count]; i = i+1) {
NSString *displayValue = [[NSString alloc] initWithString:<NEED HELP HERE>];
displayValue = [displayValue stringByAppendingString:#") "];
displayValue = [displayValue stringByAppendingString:[values objectAtIndex:i];
}
The question I have is if there is where I have put , how could I convert i to the right ASCII character (A, B, C, etc) and initialize the string with that value
NSString *displayValue = [NSString stringWithFormat:#"%c",'A'-1+i];
and to get the whole string at once, use:
NSString *displayValue = [NSString stringWithFormat:#"%c) %#",'A'-1+i,
[values objectAtIndex:i]];
(ps. if you alloc an object, you must also release or autorelease, or you will "leak" memory)
Look at NSString:initWithFormat method, along with the String Programming Guide.