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 ...
随机推荐
- Vuex入门实践(上)
一.前言 vuex被称为是专为vue应用程序开发的的状态管理模式.它的作用使用一句话描述就是:让组件之间可以共享数据 话不多少,先抛开概念,我们写一个简单的示例感受一波. 二.项目开发环境 项目开发环 ...
- OSS上传图片无法在线预览的解决方案
OSS上传图片无法在线预览的解决方案 最近在做的项目涉及到商品详情,由于前端用的flutter框架并且该详情为富文本,dart语言关于富文本的组件不是非常友好,当富文本中的图片无法在浏览器中直接预览的 ...
- 【转】15个超炫的HTML5效果
英文原文:http://www.hongkiat.com/blog/15-html5-experiments/ 翻译:iteye 乔布斯没有给Flash任何机会,微软新推出的Windows 8 ...
- Sql Server学习笔记
1.指定路径创建数据库 create database student on--创建库的时候必须写 ( name=student, filename='E:\database\student.mdf' ...
- Java控制台五子棋(纯算法)
Java五子棋小游戏 本方案是基于控制台写的一个代码 没有花里胡哨的界面,只为研究算法 仅仅用了200行代码 下面是的是运行结果 游戏运行结果 这里我就很简单的复制了一个结果 第9回合,下子方:玩家2 ...
- YOLO V3训练自己的数据集
数据的输入几乎和Faster rcnn一样,标签格式xml是一样的. 相比Faster rcnn,数据多了一步处理,通过voc_annotation.py将图片路径和bbox+class存储在txt下 ...
- A*算法知识讲解
- 最近发现Git的速度好慢
最近发现Git速度好慢,Git Bash要等5秒左右才能操作,网上查了查原因,说是火绒的问题,哎平时也用不到,看日志一个毒都没光顾过我电脑,火绒卸载! 卸了后果然快多了
- Mysql连接出现时区问题
错误提示: The server time zone value '¥x¥_¼Ð·Ç®É¶¡' is unrecognized or represents more than one time zon ...
- python requests 库 首次使用
安装requests库 执行pip3 install requests 使用resquests库获取百度网站首页 打开python idle终端.以python3为例,在终端执行python3并回车. ...