NSString 与 wchar_t*之间转化

+(NSString*)getStringFromWChar:(const wchar_t*) inStr

{

    setlocale(LC_CTYPE, "UTF-8");

    int strLength = wcslen(inStr);

    int bufferSize = (strLength+1)*4;

    char *stTmp = (char*)malloc(bufferSize);

    memset(stTmp, 0, bufferSize);

    wcstombs(stTmp, inStr, strLength);

    NSString* ret = [[[NSStringalloc] initWithBytes:stTmp length:strlen(stTmp) encoding:NSUTF8StringEncoding] autorelease];

    free(stTmp);

    return ret;

}

 

- (std::wstring) getWCharFromString

{

 

    const char  *cString;

    cString = [selfcStringUsingEncoding:NSUTF8StringEncoding];

    setlocale(LC_CTYPE, "UTF-8");

    int iLength = mbstowcs(NULL, cString, 0);

    int bufferSize = (iLength+1)*sizeof(wchar_t);

    wchar_t *stTmp = (wchar_t*)malloc(bufferSize);

    memset(stTmp, 0, bufferSize);

    mbstowcs(stTmp, cString, iLength);

    stTmp[iLength] = 0;

        printf("begin %ls",stTmp);

    std::wstring wstr(stTmp);

    free(stTmp);

    return wstr;

}

你可能感兴趣的:(NSString)