obj-c casting char * into char[]? - objective-c

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

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

How to convert const char * into a char [] array in objective C [duplicate]

I am trying to convert NSString to char str[]
i have tried this
NSString *data = #"08052678744default0000000";
char mac [[data length]];
strncpy(mac ,[data UTF8String], [data length]);
But got the wrong result.
Your help is urgently needed in resolving this. Thanks
For example:
NSString *data = #"08052678744default0000000";
const char* utf8String = [data UTF8String];
size_t len = strlen(utf8String) + 1;
char mac [len];
memcpy(mac, utf8String, len);
Note the difference between string length and byte length!

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

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 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.