#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. (推荐)linux用一键安装包

    linux一键安装包内置了XXD.apache, php, mysql这些应用程序,不需要再单独安装部署. 从7.3版本开始,linux一键安装包分为32位和64位两个包,请大家根据操作系统的情况下载 ...

  2. 非常完整的线性DP及记忆化搜索讲义

    基础概念 我们之前的课程当中接触了最基础的动态规划. 动态规划最重要的就是找到一个状态和状态转移方程. 除此之外,动态规划问题分析中还有一些重要性质,如:重叠子问题.最优子结构.无后效性等. 最优子结 ...

  3. 洛谷P1832 A+B Problem(再升级) 题解 完全背包方案计数

    题目链接:https://www.luogu.com.cn/problem/P1832 题目大意: 给定一个正整数n,求将其分解成若干个素数之和的方案总数. 解题思路: 首先找到所有 \(\le n\ ...

  4. 「2018-12-02模拟赛」T3 约束排列 解题报告

    3.约束排列(place.pas/cpp/in/out) 问题描述: 给出 n 个互不相同的小写字母,表示出现的字符类型,以及 k 个约束关系: .....,表示 ai 必须出现在 bi 前面(ai, ...

  5. 一个简单的spring boot程序

    搭建一个spring boot项目十分的方便,网上也有许多,可以参考 https://www.cnblogs.com/ityouknow/p/5662753.html 进行项目的搭建.在此我就不详细介 ...

  6. git stash使用

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/daguanjia11/article/ ...

  7. Quartz.NET总结(八)如何根据自己需要配置Topshelf 服务

    前面讲了如何使用Topshelf 快速开发windows服务, 不清楚的可以看之前的这篇文章:https://www.cnblogs.com/zhangweizhong/category/771057 ...

  8. Fabric1.4:Go 链码开发与编写

    1 链码结构 1.1 链码接口 链码启动必须通过调用 shim 包中的 Start 函数,传递一个类型为 Chaincode 的参数,该参数是一个接口类型,有两个重要的函数 Init 与 Invoke ...

  9. 升级添加到现有iOS Xcode项目的Flutter

    如果你在2019年8月之前将Flutter添加到现有iOS项目,本文值得你一看. 在2019年7月30日,合并合并请求flutter / flutter#36793之前Flutter 1.8.4-pr ...

  10. Airbnb如何应用AARRR策略成为全球第一民宿平台

    案例背景 基于房东和租客的痛点构建短租平台,但困于缓慢增长 2007年,住在美国旧金山的两位设计师——BrianChesky与Joe Gebbia正在为他们付不起房租而困扰.为了赚点外块,他们计划将阁 ...