NSString to fixed length char array conversion - objective-c

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

Related

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

UTF8String for non const string

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

NSString with Emojis

I have a NSArray containing NSStrings with emoji codes in the following format:
0x1F463
How can I now convert them into a NSString with the correct format?
With this method I am able to generate an "Emoji"-NSString:
NSString *emoji = [NSString stringWithFormat:#"\U0001F463"];
But this is only possible with constant NSStrings. How can I convert the whole NSArray?
Not my best work, but it appears to work:
for (NSString *string in array)
{
#autoreleasepool {
NSScanner *scanner = [NSScanner scannerWithString:string];
unsigned int val = 0;
(void) [scanner scanHexInt:&val];
NSString *newString = [[NSString alloc] initWithBytes:&val length:sizeof(val) encoding:NSUTF32LittleEndianStringEncoding];
NSLog(#"%#", newString);
[newString release]; // don't use if you're using ARC
}
}
Using an array of four of your sample value, I get four pairs of bare feet.
You can do it like this:
NSString *str = #"0001F463";
// Convert the string representation to an integer
NSScanner *hexScan = [NSScanner scannerWithString:str];
unsigned int hexNum;
[hexScan scanHexInt:&hexNum];
// Make a 32-bit character from the int
UTF32Char inputChar = hexNum;
// Make a string from the character
NSString *res = [[NSString alloc] initWithBytes:&inputChar length:4 encoding:NSUTF32LittleEndianStringEncoding];
// Print the result
NSLog(#"%#", res);

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.