当前位置: 代码迷 >> 综合 >> wchar_t to NSString NSString to wchar_t
  详细解决方案

wchar_t to NSString NSString to wchar_t

热度:21   发布时间:2023-12-08 10:19:56.0

wchar_t在mac intel下就是UTF-32LE,所以

wchar_t wchar[] = L"汉字";
NSString *str = [[NSString alloc] initWithBytes:wchar length:
wcslen(wchar) encoding:NSUTF32LittleEndianStringEncoding];

 

 

在iPhone中wchar_t 是32位的,所以转化的时候需要用NSUTF32StringEncoding。

下面就是转化的代码:

NSString *string = @"中国";

wchar_t *str = (wchar_t *)[string cStringUsingEncoding:NSUTF32StringEncoding];


eg.


    NSString *str = [[NSString alloc] initWithBytes:token->token().c_str() length:wcslen(token->token().c_str())*4 encoding:NSUTF32LittleEndianStringEncoding];

    NSLog(@"str = %@",str);

------------------------------------------------------------------------------------------------------------------------

wchar_t * token = (wchar_t *)[ccTokencStringUsingEncoding:NSUTF32StringEncoding];

  相关解决方案