NSString to const void * - objective-c

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;

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

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

How to calculate the hash MD5 of each content of a NSMutableArray?

I have this:
NSString *string1 = ...;
NSString *string2 = ...;
NSMutableArray *array = [NSMutableArray alloc]inithWithObjects:string1, string2]autorelease];
How do I calculate the MD5 hash (or other more appropriate hash) for each content of the array, for further comparisons?
Thanks!
You can use this method on every string of your array:
- (NSString*)md5HexDigest:(NSString*)input {
const char* str = [input UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH*2];
for(int i = 0; i<CC_MD5_DIGEST_LENGTH; i++) {
[ret appendFormat:#"%02x",result[i]];
}
return ret;
}
Do not forget to include:
#import <CommonCrypto/CommonDigest.h>

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.