#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:[山东]
//

how to convert wstring to string的更多相关文章

  1. How to: Convert Between Various String Types

      This topic demonstrates how to convert various Visual C++ string types into other strings. The str ...

  2. C++中Cstring、wstring 和string互相转换总结

    通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...

  3. CString 转 char*; wstring 转 string

    1. CString  转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...

  4. itoa : Convert integer to string

      Quote from:  http://www.cplusplus.com/reference/cstdlib/itoa/   function   Required header : <s ...

  5. VC++中 wstring和string的互相转换实现

    在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...

  6. wchar_t与char、wstring与string的相互转换

    个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...

  7. ToString()、Convert.ToString()、(string)、as string 的区别

    通常 object 到 string 有四种方式(假设有object obj):obj.ToString().Convert.ToString().(string)obj.obj as string. ...

  8. how convert large HEX string to binary array ?

    how convert large HEX string to binary I have a string with 14 characters . This is a hex represanta ...

  9. How to convert a std::string to const char* or char*?

    How to convert a std::string to const char* or char*? 1. If you just want to pass a std::string to a ...

随机推荐

  1. 26.python操作Excel

    写入Excel import xlwt book=xlwt.Workbook(encoding='utf-8') sheet=book.add_sheet('xiangxin') # 标题名 titl ...

  2. 解释一下什么是 aop?(未完成)

    解释一下什么是 aop?(未完成)

  3. C++ | C++ 基础知识 | 类型与声明

    一.类型 C++ 包含一整套基本类型,这些类型对应计算机最基本的存储单元并且展现 1.0 布尔值 一个布尔变量(bool)的取值或者是 true 或者是 false,布尔变量常用于表达逻辑运算结果. ...

  4. Tomcat的性能优化及JVM内存工作原理

    JVM性能优化原则:代码运算性能.内存回收.应用配置(影响Java程序主要原因是垃圾回收机制)代码层优化:避免过多循环嵌套.调用和复杂逻辑. Tomcat调优主要内容 1.增加最大连接数 2.调整工作 ...

  5. echarts更改坐标轴文字颜色及大小

    xAxis: { data: anameArr, axisLabel: { show: true, textStyle: { color: '#c3dbff', //更改坐标轴文字颜色 fontSiz ...

  6. Nginx作为负载均衡服务器——server参数讲解

    upstream举例 upstream backend { server backend1.ecample.com weight = 5; # wwight 代表权重 server backend2. ...

  7. 关于爬虫的日常复习(18)——scrapy系列3

  8. Nginx的踩坑实录

    1.昨天在为一个新项目配置地址转发,搞了很久都没生效,日志也没有问题,但就是没到转发的目标机器上. nginx.conf 配置如下: location /prism{ proxy_pass http: ...

  9. Oracle GoldenGate to Confluent with Kafka Connect

    Confluent is a company founded by the team that built Apache Kafka. It builds a platform around Kafk ...

  10. 调试 ambari-server 总结

    刚开始debug ambari-server的时候,很多逻辑都是第一次接触.其中有很多知识点还是记录一下的好,做个备忘.这些知识点对于自定义api的开发还是很有作用的. 1. api的子href的最后 ...