Storing data in string into an integer array - objective-c

I have some data into a string and I wish to store that data in an integer array... Below is the code.
int valMines[256];
// 'b' is NSString with 256 values in it.
for(int i=0; i<[b length]; i++){
valMines[i] = [NSString stringWithFormat:#"%d", [b characterAtIndex:i]];
NSLog(#"valMines1 is %#", valMines[i]);
}
I am getting a warning and due to that my application is not getting loaded:
Assignment makes integer from pointer without a cast.
Please help

Your valMins is an integer array and you are assigning NSString to it. Probably you are looking something like this:
unichar valMines[256]; // make it unichar instead of int
// 'b' is NSString with 256 values in it.
for(int i=0; i<[b length]; i++){
valMines[i] = [b characterAtIndex:i]; // get and store the unichar
NSLog(#"valMines1 is %d", valMines[i]); // format specifier is %d, not %#
}

Related

divide string into characters

I have an NSString *titleName which changes according to an if statement. So the length (number of characters) in the string changes. I would like to divide titleName into a MutableArray of separate strings consisting of its individual characters. I would then like to use these separate strings as the text in different UILabels. I am not sure as how to go about this.
Through some research I have tried to create the NSMutable array like this
NSMutableArray *letterArray = substringWithRange:((i = 0);i<[titleName2 length];i++));
but this gives me an error Use of undeclared identifier 'substringWithRange.
Can someone help me.
I decided to use componentsSeparatedByString instead and just created my various strings with a , between each letter. Thanks for anybody's thoughts though.
The code you pasted is not valid objective-C.
To keep the same algorithm you should write something like :
NSMutableArray *letterArray = [NSMutableArray array];
NSUInteger length = [titleName2 length];
for (NSUInteger i = 0; i < length; ++i) {
[letterArray addObject:[titleName2 substringWithRange:NSMakeRange(i, 1)]];
}
It's probably much "cheaper" to hold a C-array of unichar characters that make-up the string. It will also be quicker to create:
NSString *input = #"How now brown cow?";
unichar chars[[input length]];
for (NSInteger i = 0; i < [input length]; i++)
chars[i] = [input characterAtIndex:i];
Alternatively you could use malloc() to create the C-array:
NSString *input = #"How now brown cow?";
unichar *chars = (unichar *)malloc([input length]);
for (NSInteger i = 0; i < [input length]; i++)
chars[i] = [input characterAtIndex:i];
and then use free(), later, to, err, free the memory:
free(chars);
Cheaper still, would be to not split-up the string at all...
Try this below code
NSMutableArray *letterArray = [NSMutableArray array];
for (int i = 0;i<[titleName2 length];i++)
{
[letterArray addObject: [titleName2 substringWithRange:NSMakeRange(i,1)]];
}
DLog(#"%#", letterArray);
Other option to get characters of string
NSMutableArray *letterArray = [NSMutableArray array];
for (int i=0; i < [titleName2 length]; i++)
{
[letterArray addObject:[NSString stringWithFormat:#"%c", [titleName2 characterAtIndex:i]]];
}
DLog(#"characters - %#", letterArray);

Convert one NSArray filled with NSStrings to a UTF8String vector

So I was wondering, is there some quick way of converting one NSArray filled with NSStrings to the equivalent UTF8string values?
I want to store some parameter configuration in a NSArray and then use them in a function that takes (int argv, const char *argv[]) as arguments.
I implemented this in a convoluted way
int argc = [gameParameters count];
const char **argv = (const char **)malloc(sizeof(const char*)*argc);
for (int i = 0; i < argc; i++) {
argv[i] = (const char *)malloc(sizeof(char)*[[gameParameters objectAtIndex:i] length]+1);
strncpy((void *)argv[i], [[gameParameters objectAtIndex:i] UTF8String], [[gameParameters objectAtIndex:i] length]+1);
}
but I'm not really happy with and cleaning up memory is tedious.
Do you know a better way to achieve this result?
Your current implementation is not correct if the string contains non-ASCII characters. For example, the string #"é" (SMALL LETTER E WITH ACUTE) has length 1, but the UTF-8 sequence "C3 A9" has 2 bytes. Your code would not allocate enough memory for that string.
(In other words: [string length] returns the number of Unicode characters in the string, not the number of bytes of the UTF-8 representation.)
Using strdup(), as suggested by Kevin Ballard, would solve this problem:
argv[i] = strdup([[gameParameters objectAtIndex:i] UTF8String]);
But you should also check if duplicating the strings is necessary at all. If you call the function in the current autorelease context, the following would be sufficient:
int argc = [gameParameters count];
const char **argv = (const char **)malloc(sizeof(const char*)*argc);
for (int i = 0; i < argc; i++) {
argv[i] = [[gameParameters objectAtIndex:i] UTF8String];
}
yourFunction(argc, argv);
free(argv);

XOR'ing two hex values stored as an NSString?

here is yet another silly question from me!
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
/* some stuff like result = hex1^hex2; */
NSString *result = #"6001269f7d";
I have a hex value as a string, stored in two diff. variables. i need to Xor them and the result should be in another string variables?
i tried them by converting string --> NSData --> bytes array --> xor'ing them ...but i have no success.....
thank you in advance...
You have to convert every character to Base16(for hexadecimal) format first.Then you should proceed with XORing those characters.You can use the strtol() function to achieve this purpose.
NSString *hex1 = #"50be4f3de4";
NSString *hex2 = #"30bf69a299";
NSMutableArray *hexArray1 = [self splitStringIntoChars:hex1];
NSMutableArray *hexArray2 = [self splitStringIntoChars:hex2];
NSMutableString *str = [NSMutableString new];
for (int i=0; i<[hexArray1 count]; i++ )
{
/*Convert to base 16*/
int a=(unsigned char)strtol([[hexArray1 objectAtIndex:i] UTF8String], NULL, 16);
int b=(unsigned char)strtol([[hexArray2 objectAtIndex:i] UTF8String], NULL, 16);
char encrypted = a ^ b;
NSLog(#"%x",encrypted);
[str appendFormat:#"%x",encrypted];
}
NSLog(#"%#",str);
Utility method that i used to split characters of the string
-(NSMutableArray*)splitStringIntoChars:(NSString*)argStr{
NSMutableArray *characters = [[NSMutableArray alloc]
initWithCapacity:[argStr length]];
for (int i=0; i < [argStr length]; i++)
{
NSString *ichar = [NSString stringWithFormat:#"%c", [argStr characterAtIndex:i ]];
[characters addObject:ichar];
}
return characters;
}
Hope it helps!!

How to parse out each Chinese character?

Given a sentence composing of X number of Chinese characters. I want to parse each character out in Objective-C or C++.
I tried:
NSString * nsText = [NSString stringWithFormat:#"你好吗"];
for (int i = 0; i < [nsText length]; i++)
{
char current = [nsText characterAtIndex:i];
printf("%i: %c\n", i, current);
}
But I'm not getting the right characters, I got index 0 = ', index 1 = }, etc. The length is returned correctly, which equals 3. I need UTF8 encoding to display it to the UI.
Any tips will be helpful.
Thank you
Three things wrong. First, characterAtIndex: returns a unichar, which is bigger than the char to which you're assigning. You're losing information there. Second, %c is the format specifier for printing an ASCII value (8 bits). You want %C (uppercase 'C') to print a 16-bit unichar. Finally, printf() doesn't seem to accept %C, so you need to use NSLog() instead. Rewritten, then, we have:
NSString * nsText = [NSString stringWithFormat:#"你好吗"];
for (int i = 0; i < [nsText length]; i++)
{
unichar current = [nsText characterAtIndex:i];
NSLog(#"%i: %C\n", i, current);
}
Can this solve your problem?
NSString * nsText = [NSString stringWithFormat:#"你好吗"];
for (int i = 0; i < [nsText length]; i++) {
NSString *str = [nsText substringToIndex:i+1];
NSString *str2 =[str substringFromIndex:i];
NSLog(#"%#",str2);
}

Objective-C Iterating through an NSString to get characters

I have this function:
void myFunc(NSString* data) {
NSMutableArray *instrs = [[NSMutableArray alloc] initWithCapacity:[data length]];
for (int i=0; i < [data length]; i++) {
unichar c = [data characterAtIndex:i];
[instrs addObject:c];
}
NSEnumerator *e = [instrs objectEnumerator];
id inst;
while (inst = [e nextObject]) {
NSLog("%i\n", inst);
}
}
I think it fails at [instrs addObject:c]. It's purpose is to iterate through the hexadecimal numbers of an NSString. What causes this code to fail?
A unichar is not an object; it's an integer type.
NSMutableArray can only hold objects.
If you really want to put it into an NSMutableArray, you could wrap the integer value in an NSNumber object: [instrs addObject:[NSNumber numberWithInt:c]];
But, what's the point of stuffing the values into an array in the first place? You know how to iterate through the string and get the characters, why put them into an array just to iterate through them again?
Also note that:
the "%i" NSLog format expects an integer; you can't pass it an object
for hexadecimal output, you want "%x", not "%i"
If the function is only meant to display the characters as hexadecimal values, you could use:
void myFunc(NSString* data)
{
NSUInteger len = [data length];
unichar *buffer = calloc(len, sizeof(unichar));
if (!buffer) return;
[data getCharacters:buffer range:NSMakeRange(0, len)];
for (NSUInteger i = 0; i < len; i++)
NSLog(#"%04x", (unsigned) buffer[i]);
free(buffer);
}
This is just a little bit more efficient than your approach (also, in your approach you never release the instrs array, so it will leak in a non-garbage-collected environment).
If the string contains hexadecimal numbers, then you will want to repeatedly use an NSScanner's scanHexInt: method until it returns NO.
void myFunc(NSString* data)
{
NSScanner *scanner = [[NSScanner alloc] initWithString:data];
unsigned number;
while ([scanner scanHexInt:&number])
NSLog(#"%u", number);
[scanner release];
}