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

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

Related

NSString to const void *

How to convert NSString to const void?
I've tried
NSString *testString = #"testString";
const void *testConstVoid = CFBridgingRetain(testString);
But testConstVoid is still an nsstring
Instead of CFBridgingRetain(testString), you should use testString.UTF8String
NSString *testString = #"testString";
const void *testConstVoid = testString.UTF8String;
Result
In addition to UTF8String you have cStringUsingEncoding: and getCString:maxLength:encoding:.
In the first 2 cases you need to copy the returned C string to your own char buffer or NSData if you plan to use it past the original NSString lifetime:
NSString *str = #"hello";
const char *cStr = str.UTF8String;
const NSUInteger cStrLen = [str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
NSMutableData *cStrData = [[NSMutableData alloc] initWithBytes:cStr
length:cStrLen + 1];
// +1 for '\0' (C string null-terminator)
const void *cStrBytes = cStrData.bytes;

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

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

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.