#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <fstream>
#include <sstream>
#include <afxwin.h>
using namespace std; int main()
{
setlocale(LC_CTYPE, "chs");
string str;
wstring wstr = L"123呵呵呵abc";
char cstr[100] = {0};
sprintf(cstr, "%S", wstr.c_str());
str = cstr;
cout << str << endl;
}

写入文件

#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <fstream> int main(int argc, char *argv[])
{
std::wstring str = L"123,我是谁?我爱钓鱼岛!"; std::wstring_convert<std::codecvt_utf8<wchar_t>> conv; std::string narrowStr = conv.to_bytes(str);
{
std::ofstream ofs ("c:\\test.txt");
ofs << narrowStr;
} std::wstring wideStr = conv.from_bytes(narrowStr);
{
std::locale::global(std::locale("Chinese-simplified"));
std::wofstream ofs (L"c:\\testW.txt");
ofs << wideStr;
}

UTF8文件读取

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
#include <fstream>
#include <sstream>
#include <afxwin.h>
using namespace std; int main()
{
auto LocUtf8 = locale(locale(""),new codecvt_utf8<wchar_t>);
wifstream wfin("test.txt");
wfin.imbue(LocUtf8);
wstring wstr;
while (getline(wfin, wstr))
{
wcout.imbue(locale(""));
wcout << wstr << endl;
}
wfin.close();
}

假如我们取到 的数据是这样的:

{"ret":1,"start":"58.57.64.0","end":"58.57.95.255","country":"\u4e2d\u56fd","province":"\u5c71\u4e1c","city":"\u6f4d\u574a","district":"","isp":"\u7535\u4fe1","type":"","desc":""}

http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=58.57.91.184

那么我们改怎么样进行转化呢?

zhaoshizhong1老师的方法:

#include <cstdio>
#include <locale>
#include <iostream>
using namespace std; #define MAXL 100
char u[]="\\u4e2d\\u56fd", *p;
wchar_t us[MAXL];
char str[MAXL];
int i;
int main()
{
setlocale(LC_ALL, "chs");
//_wsetlocale(LC_ALL, L"chs");
//std::locale loc = (std::locale("Chinese-simplified"));
i = 0;
p = u;
while (true)
{
if (1 != sscanf(p, "\\u%4hx", &us[i])) break;
i++;
if (i >= MAXL - 1) break;
p += 6;
}
us[i] = 0;
//wcout.imbue(loc);
sprintf(str, "%S", us);
//wcout << us << endl;
cout << str << endl;
return 0;
}
//u:[\u5c71\u4e1c]
//us:[山东]
//

最新文章

  1. Oracle数据库的链接数目超标
  2. Endnote专题之--output style相关问题
  3. Tomcat启动脚本
  4. wireshark排查打印机问题
  5. Windbg 内存命令 《第四篇》
  6. 利用MVC编程模式-开发一个简易记事本app
  7. 《APUE》第三章笔记(4)及习题3-2
  8. 【prism】前期准备
  9. Jquery插件之信息弹出框showInfoDialog(成功、错误、警告、通知)
  10. Spreadsheets
  11. H5 表单元素
  12. 错误:Invalid action class configuration that references an unknown class named [XXX]的解决
  13. PHP内核之旅-6.垃圾回收机制
  14. Codeforces Round #523 (Div. 2) C Multiplicity (DP)
  15. POJ 2593
  16. Docker常用命令&lt;转&gt;
  17. Python并发讨论
  18. 【leetcode 简单】 第八十一题 4的幂
  19. Redis学习(4)-数据类型,string,hash
  20. CSS样式表——布局练习(制作360网页)

热门文章

  1. 集合排序Comparable和Comparator有什么区别?
  2. Docker日常常用命令汇总
  3. 2019年面试官最喜欢问的28道ZooKeeper面试题
  4. CF825G Tree Queries
  5. 虚拟机安装(Vmware14)
  6. web实现点击左侧导航,右侧加载不同的网页(这种布局多用于后台管理系统)
  7. cogs 49. 跳马问题 DFS dp
  8. ELK实战-elasticsearch安装
  9. MNIST数据集
  10. linux 为动态分配的Virtualbox虚拟硬盘扩容