UTF8String for non const string - objective-c

I found a way to set a string to a char array. I use the following code:
NSString *string = [[NSString alloc] initWithFormat:#"This is a string!"];
char *c = [string UTF8String];
However, I get the warning, "Initializing 'char ' with an expresion type of 'const char' discards qualifiers.
It works fine, but I feel like there's a better way to do it, and I'd rather not have a ton of warnings. Does anyone have any suggestions?

The value returned by [NSString UTF8String] is a pointer to a constant array of chars. You cannot remove "const" directly.
If you want a non-constant array of chars you must copy the string using the method strcpy(...). You have to declare and initialize an array of chars and then copy the value of the given string inside:
NSString *string = #"This is a string";
char cString [string.length]
strcpy(cString, string.UTF8String);
NSLog(#"Copied String: %s", cString);
or if you prefer to manage the memory yourself:
NSString *string = #"This is a string";
char * cString = malloc(sizeof(char)*string.length);
strcpy(cString, string.UTF8String);
NSLog(#"Copied String: %s", cString);
free(cString);

Use const char and you can use different types of encoding also:
const char* c = [string cStringUsingEncoding:NSUTF8StringEncoding];

NSFileManager *fileMgr = [NSFileManager defaultManager];
if (![fileMgr fileExistsAtPath:pathOfDB]) {
const char *pathDB = [pathOfDB UTF8String];

Related

Comparing strings is giving false in Objective-C

I am comparing two strings using isEqual method and it is giving NO.
if ([trie.name isEqual:string]) {
isFound = YES;
break;
}
The trie.name is a string initialized using [[NSString alloc] initWithCharacters:&uchar length:ucharLen], where uchar is a unichar. The string is a constant string #"N". I have checked the hash values and both differ. I am not sure why creating a string using different initializer gives different hash for the same string value. In this case, how to check for string equality?
Please see the attached screenshot of the debug variables.
Currently, I am using the code:
NSString *string = #"Pirates";
unichar uchar = [string characterAtIndex:0];
size_t ucharLen = sizeof(uchar);
NSString *str = [[NSString alloc] initWithCharacters:&uchar length:ucharLen];
XCTAssertTrue([str isEqual:#"P"]);
If I given the length: as 1, it works properly. How to get the length of the unichar. Will it always be 1 so that I can hardcode 1 in this case?
The problem is with:
unichar uchar = [string characterAtIndex:0];
size_t ucharLen = sizeof(uchar);
NSString *str = [[NSString alloc] initWithCharacters:&uchar length:ucharLen];
You are creating a string from a single unichar which means the length needs to be 1. But the sizeof(unichar) returns 2 since unichar is a 16-bit (2-byte) value so you end up telling the string you are passing 2 characters, not 1.
So the resulting string contains two characters - the one you actually want and a second, random bit of garbage that happens to be at that memory address.
Just hardcode 1:
unichar uchar = [string characterAtIndex:0];
NSString *str = [[NSString alloc] initWithCharacters:&uchar length:1];

obj-c casting char * into char[]?

i've seen the question asked for other languages, but not objective-c specifically. how would i cast char * into char []?
CODE:
NSString *thisString = #"48454C4C4F";
char *charString = [thisString UTF8String];
NSLog(#"%s",charString);
// output: 48454C4C4F
// how could i convert into char [] = {48,45,etc.)
You can do it like this:
NSString *s = #"Some string";
const char *c = [s UTF8String];
You could also use
-[NSString cStringUsingEncoding:]
if your string is encoded with something other than UTF-8.
Once you have the const char *, you can use it as an array of chars:
printf("%c\n", c[5]);
If you want to modify the string, make a copy:
char *cpy = strdup(c);
// Work with copy
free(cpy);
If you must have an actual char array you can do it like this:
NSString *data = #"Some string";
const char* utf8String = [data UTF8String];
size_t len = strlen(utf8String) + 1;
char mac [len];
memcpy(mac, utf8String, len);

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;
};

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);

NSString get -characters

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.