NSString to wchar_t & wchar_t to NSString

-(NSString*)getStringFromWChar:(const wchar_t*) inStr{
    char *outStr = NULL;
    size_t size = wcstombs(NULL, inStr, 0);
    outStr = new char[size + 1];
    memset(outStr, 0, size * sizeof(char));
    size_t ret = wcstombs(outStr, inStr, size+1);
    if (ret == -1) {
        delete[] outStr;
        outStr = NULL;
    }
    NSString *string = [NSString stringWithCString:outStr encoding:NSUTF8StringEncoding];   
    return string;
}

-(wchar_t*)getWCharFromString:(NSString *)inStr{
    const char *cString = [inStr cStringUsingEncoding:NSUTF8StringEncoding];
    setlocale(LC_CTYPE, "UTF-8");
    wchar_t *outStr = NULL;
    size_t size = mbstowcs(NULL, cString, 0);
    outStr = new wchar_t[size + 1];
    if (outStr) {
        memset(outStr, 0, size * sizeof(wchar_t));
        size_t ret = mbstowcs(outStr, cString, size+1);
        if (ret == -1) {
            delete[] outStr;
			outStr = NULL;
        }
    }
    return outStr;
}

你可能感兴趣的:(String,null,delete,encoding)