how can i use the UTF8string for the NSArray - iphone

I have taken char data into database into array. now i want to convert that data into string.
how can i convert array data into NSString.

If you have a const char * instance, you can use the NSString method + stringWithCString:encoding:. For example:
NSString *_myString = [NSString stringWithCString:_myCharPtr encoding:NSUTF8StringEncoding];
To put that into an NSArray*, you might do the following:
NSArray *_myArray = [NSArray arrayWithObjects:_myString,nil];

use
[NSString stringWithUTF8String:<#(const char *)nullTerminatedCString#>]

Related

Convert a hash to NSString?

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.

objective c getting substring after and before dot

i have a string like 112.1 or 102.2 etc now i want to get the substring before dot and after the dot using objective c, means i want to get 2 substrings like this 112 and 1. please guide. Regards Saad.
Try the following:
NSArray *arrayWithTwoStrings = [yourString componentsSeparatedByString:#"."];
Using this method you'll get NSArray containing string components that were separated by "." string, that is "112" and "1" for "112.1" string. After that you can access them using array's objectAtIndex: method.
P.S. Note also that if you're going to use numeric values of those strings there might be better solution
NSString *str = yourstr;
NSArray *Array = [str componentsSeparatedByString:#"."];
NSString *t = [Array objectAtIndex:0];
NSString *t1 = [Array objectAtindex:1];
NSArray* components = [yourString componentsSeparatedByString:#"."];

Converting a uint8_t array to an NSString

How can I affect a uint8_t array (see decryptedBuffer below) to an NSString?
uint8_t *decryptedBuffer;
NSString *cle2=[NSString stringWithUTF8String:decryptedBuffer];
NSString *str2=[player.name AES256DecryptWithKey:cle2];
NSLog(str2);
free(plainBuffer);
free(cipherBuffer);
free(decryptedBuffer);
uint8_t * is just a byte string which is compatible with char *, so you should just be able to pass the casted pointer to stringWithUTF8String, assuming the decrypted string is UTF-8 and it is NULL terminated:
NSString *s = [NSString stringWithUTF8String:(char *)decryptedBuffer];
If the data is not NULL terminated, you can use this:
NSString *s = [[[NSString alloc] initWithBytes:decryptedBuffer
length:length_of_buffer
encoding:NSUTF8StringEncoding] autorelease];
decryptedBuffer is an int (uint8_t), NSString stringWithUTF8String only works on strings, not ints. I think I found what you need: http://lists.apple.com/archives/cocoa-dev/2004/Apr/msg01437.html
That person used this syntax:
NSString *theDigitsIWant = [[NSNumber numberWithInt:x] stringValue];
So you should do this:
NSString *cle2 = [[NSNumber numberWithInt:decryptedBuffer] stringValue];

Split one string into different strings

i have the text in a string as shown below
011597464952,01521545545,454545474,454545444|Hello this is were the message is.
Basically i would like each of the numbers in different strings to the message eg
NSString *Number1 = 011597464952
NSString *Number2 = 01521545545
etc
etc
NSString *Message = Hello this is were the message is.
i would like to have that split out from one string that contains it all
I would use -[NSString componentsSeparatedByString]:
NSString *str = #"011597464952,01521545545,454545474,454545444|Hello this is were the message is.";
NSArray *firstSplit = [str componentsSeparatedByString:#"|"];
NSAssert(firstSplit.count == 2, #"Oops! Parsed string had more than one |, no message or no numbers.");
NSString *msg = [firstSplit lastObject];
NSArray *numbers = [[firstSplit objectAtIndex:0] componentsSepratedByString:#","];
// print out the numbers (as strings)
for(NSString *currentNumberString in numbers) {
NSLog(#"Number: %#", currentNumberString);
}
Look at NSString componentsSeparatedByString or one of the similar APIs.
If this is a known fixed set of results, you can then take the resulting array and use it something like:
NSString *number1 = [array objectAtIndex:0];
NSString *number2 = [array objectAtIndex:1];
...
If it is variable, look at the NSArray APIs and the objectEnumerator option.
NSMutableArray *strings = [[#"011597464952,01521545545,454545474,454545444|Hello this is were the message is." componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#",|"]] mutableCopy];
NString *message = [[strings lastObject] copy];
[strings removeLastObject];
// strings now contains just the number strings
// do what you need to do strings and message
....
[strings release];
[message release];
does objective-c have strtok()?
The strtok function splits a string into substrings based on a set of delimiters.
Each subsequent call gives the next substring.
substr = strtok(original, ",|");
while (substr!=NULL)
{
output[i++]=substr;
substr=strtok(NULL, ",|")
}
Here's a handy function I use:
///Return an ARRAY containing the exploded chunk of strings
///#author: khayrattee
///#uri: http://7php.com
+(NSArray*)explodeString:(NSString*)stringToBeExploded WithDelimiter:(NSString*)delimiter
{
return [stringToBeExploded componentsSeparatedByString: delimiter];
}

how to concatenate values to the string

i want to cancatenate strings with comma as a separator and result must be stored in string...
comma=#",";
for(i=0;i<[resources count];i++)
{
Record *aRecord = [resources objectAtIndex:i];
temp=aRecord.programID;
if(i==0)
pid=temp;
else
//i am using this one to cancatenate but is not working why?
pid = [NSString stringWithFormat:#"%#%#%#", pid,comma,temp];
}
Use the -componentsJoinedByString: method on NSArray:
NSArray *csvArray = [NSArray arrayWithObjects:#"here", #"be", #"dragons", nil];
NSLog(#"%#", [csvArray componentsJoinedByString:#", "]);
(from the docs)
Cast the id types to NSString and then use the concatenation methods found in the class reference of NSString.