Mac 版:
- //
- // main.m
- // test
- //
- // Created by haidrgon on 2018/12/20.
- // Copyright ? 2018 haidrgon. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #include<iostream>
- #include <string>
- #include <stdlib.h>
- #import<cwchar>
- #import<wchar.h>
- using namespace std;
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- // insert code here...
- wchar_t* temp=L"abc123 你我他";
- wstring wstr = temp;
- wprintf(L"%s\n", wstr.c_str());
- // wcout.imbue(locale("chs"));
- wcout<<wstr<<endl;
- NSData *data = [NSData dataWithBytes:wstr.data() length:sizeof(wchar_t) * wstr.size()];
- NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF32LittleEndianStringEncoding];
- NSLog(@"str %@", str);
- NSLog(@"Hello, World!");
- cout<<[str UTF8String]<<endl;
- }
- return 0;
- }
Linux 版
- #include<iostream>
- #include<string>
- #include<locale>
- using namespace std;
- int main()
- {
- wstring wstr=L"世界你好!";
- locale loc("zh_CN.UTF-8");
- locale::global(loc);
- wcout<<wstr<<endl;
- return 0;
- }
来源: http://www.bubuko.com/infodetail-2891761.html