how to convert wstring to string
#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的更多相关文章
- How to: Convert Between Various String Types
This topic demonstrates how to convert various Visual C++ string types into other strings. The str ...
- C++中Cstring、wstring 和string互相转换总结
通过前一篇文章<C++中string,wstring,CString的基本概念和用法>,对Cstring.wstring 和string有了一个了解.string是C++提供的标准字符串操 ...
- CString 转 char*; wstring 转 string
1. CString 转 char* ); CString name; name.Format(_T("bookUC%d.txt"),m_ID); std::wstring _n ...
- itoa : Convert integer to string
Quote from: http://www.cplusplus.com/reference/cstdlib/itoa/ function Required header : <s ...
- VC++中 wstring和string的互相转换实现
在VC++开发中,经常会用到string和wstring,这就需要二者之间的转换,项目中封装了wstring和string相互转换的2个函数,实现如下: //将wstring转换成string std ...
- wchar_t与char、wstring与string的相互转换
个人倾向于使用优秀的开源库做这个. 最近使用boost进行转换,代码极其简单: boost::filesystem::path src(wchar_t); char = src.string().c_ ...
- ToString()、Convert.ToString()、(string)、as string 的区别
通常 object 到 string 有四种方式(假设有object obj):obj.ToString().Convert.ToString().(string)obj.obj as string. ...
- 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 ...
- 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 ...
随机推荐
- JVM探秘:Java对象
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 对象的创建 虚拟机遇到一条 new 指令时,首先去检查这个指令的参数是否能在方法区常量 ...
- AutoCad 二次开发 Jig操作之墙块的拖动
测试结果: 主要思路:选择一段多段线,使用封装的jig类进行实时拖动,其原理就是在拖动的时候,确定被拖动的边,我是选择离输入第一个点最近的边作为拖动边,有了这条边,就能确定需要实时更改的点了,然后当鼠 ...
- .Net Core 认证系统之基于Identity Server4 Token的JwtToken认证源码解析
介绍JwtToken认证之前,必须要掌握.Net Core认证系统的核心原理,如果你还不了解,请参考.Net Core 认证组件源码解析,且必须对jwt有基本的了解,如果不知道,请百度.最重要的是你还 ...
- Alodi:环境创建从未如此简单
一个满足你各种想象的快速方便生成临时环境的系统 在『Alodi:为了保密我开发了一个系统』文章中有讲到我们开发了一个系统用来快速生成临时测试环境,短短三个月已有数百个环境被创建,简化了工作,节省了时间 ...
- TensorFlow——训练模型的保存和载入的方法介绍
我们在训练好模型的时候,通常是要将模型进行保存的,以便于下次能够直接的将训练好的模型进行载入. 1.保存模型 首先需要建立一个saver,然后在session中通过saver的save即可将模型保存起 ...
- python关系(比较)运算符
关系运算符 就是 比较运算符 a.对象的值进行比较 数字间的比较运算符连着使用: 数字与True.False的比较True 表示 1 , False 表示 0 数字与字符串的比较(不能比较) 字符串间 ...
- cogs 826. [Tyvj Feb11] GF打dota 次短路详细原创讲解! dijkstra
826. [Tyvj Feb11] GF打dota ★★☆ 输入文件:dota.in 输出文件:dota.out 简单对比时间限制:1 s 内存限制:128 MB 众所周知,GF同学喜 ...
- target 和 currentTarget的区别
target是当前点击的组件,currentTarget是扑捉到事件的组件
- java.sql.SQLException: connection holder is null 问题处理
问题描述 上上个周测试的时候突然报系统异常,于是我立即查看日志,发现是一个数据库异常:java.sql.SQLException: connection holder is null我第一想到的就是可 ...
- 去除Linux中的^M
(1)安装tofrodos sudo apt-get install tofrodos (2)做一些优化 ln -s /usr/bin/todos /usr/bin/unix2dos ln -s /u ...