NSString get -characters - objective-c

I want to get the characters of an NSString. Like this:
NSString *data;
const char * typein = [[data characters] UTF8String];
But obviously NSString won't respond to -characters. How do I get the characters of NSString?
thanks,
Elijah

You can use this function:
for(int i =0 ;i<[myString length]; i++) {
char character = [myString characterAtIndex:i];
}
or
NSString *str = #"astring";
const char *cString = [str UTF8String];

If you just want to get a cString from the NSString just call UTF8String as you are already doing and then iterate the array.

Related

How to convert Objective-C string into a C string?

I need to convert an C-String into an NSString.
How do I do this?
I know how to convert it the OTHER WAY,
NSString *hello = #"Hello!";
const char *buffer;
buffer = [schoolName cStringUsingEncoding: NSUTF8StringEncoding];
NSLog(#"C-String is: %s", buffer);
However, how do I do it Objective-C string (NSString) into a NULL-TERMINATED string.
Thanks!
const char *buffer = [hello UTF8String]; will do what you're looking for.
Now to answer the new (and very different) question:
If you have, for example, const char *cstring = "hello world"; you can create an NSString * with it through: NSString *nsstring = [NSString stringWithFormat:#"%s", cstring];
There are, of course, other ways to accomplish the same thing.
NSString* str = [NSString stringWithUTF8String:(const char *)]
or
NSString* str = [NSString stringWithCString:(const char *) encoding:(NSStringEncoding)]
or
NSString* str = [NSString stringWithCharacters:(const unichar *) length:(NSUInteger)]
Try something like this:
- (wchar_t*)getWideString
{
const char* temp = [schoolName cStringUsingEncoding:NSUTF8StringEncoding];
int buflen = strlen(temp)+1; //including NULL terminating char
wchar_t* buffer = malloc(buflen * sizeof(wchar_t));
mbstowcs(buffer, temp, buflen);
return buffer;
};

Convert text in an NSString to it's 8-byte ASCII Hex equivalent, and store back in an NSString

I want to convert the NSString #"2525" to the NSString #"0032003500320035". The 8-byte ASCII value for "2" in hex is "0032" and for "5" it's "0035". Just to get the c-string equivalent, I tried...
const char *pinUTF8 = [pin cStringUsingEncoding:NSASCIIStringEncoding];
...but as you can see I'm struggling with this and I knew it wasn't going to be that easy. Any tips?
Thanks so much in advance for your wisdom!
Try this:
NSString *str = #"2525";
const char *s = [str cStringUsingEncoding:NSASCIIStringEncoding];
size_t len = strlen(s);
NSMutableString *asciiCodes = [NSMutableString string];
for (int i = 0; i < len; i++) {
[asciiCodes appendFormat:#"%04x", (int)s[i]];
}
NSLog(#"%#", asciiCodes);

NSString to fixed length char array conversion

I am having a struct which contains a char array like this:
char name[10];
Now I need a way to convert NSString to this type of string.
I already can convert this string to a NSString:
NSString *string = [[NSString alloc] initWithBytes:name length:10
encoding:NSASCIIStringEncoding];
You can do this with getCString:maxLength:encoding: method of NSString:
char name[10];
NSString *s = #"Hello";
[s getCString:name maxLength:10 encoding:NSASCIIStringEncoding];
the full conversion process back and forth:
// from NSString through char* to NSString again
NSString *text = #"long text or short";
const char *constName = [text UTF8String];
char *name = malloc(sizeof(unichar) * text.length + 1);
memcpy(name, constName, sizeof(unichar) * text.length);
NSString *fromChar = [NSString stringWithUTF8String:name];
NSLog(#"%#", fromChar);
free(name);

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 "concat" bytes from NSString and its length together?

I have a NSString [WORD] that has some length [LEN]. What i need to do is to get bytes from this string and put them together with length in short (2 bytes), so i would have [WORD] [LEN].
E.g.
String "AB" in utf8 HEX is 4142. Length of this string is 2==> 0002 in HEX.
So everything together is 41420002. How to get this bytes together?
I think this code does what you want.
NSString *myString = #"AB";
const char *chars = [myString UTF8String];
NSMutableString * result = [NSMutableString string];
for (int i=0; i < [myString length]; i++) {
[result appendFormat:#"%X", chars[i]];
}
[result appendFormat:#"%04X", [myString length]];
NSLog(#"%#", result);
Hope it helps!