how to convert wstring to string
来源:互联网 发布:php将数组转化成json 编辑:程序博客网 时间:2024/05/18 00:35
#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 100char 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 InputStream to String/convert char[] to String
- How to convert string to Guid type.
- How to convert a String to DateTime
- How to convert QString to std::string?
- How to properly convert SecureString to String
- string to wstring
- How to: Convert Between Various String Types
- How to: Convert Between Various String Types
- How to: Convert Between Various String Types
- How to convert Java String into byte[]?
- How to: Convert System::String to wchar_t* or char*
- How to convert std::string to LPCWSTR in C++ (Unicode)
- How to convert from int to string in objective c
- How to convert from int to string in objective c
- How to convert char array to String in Java
- Swift: How to convert a String to UInt8 array?
- Visual C++ How to: Convert Between Various String Types
- 【后缀自动机-后缀树上的维护】hdu4641
- Android开发中的多线程编程技术
- 黑马程序员---基础语法(二)
- eclipse控制台输出到文件
- TortoiseSVN文件夹及文件图标不显示解决方法
- how to convert wstring to string
- sql server 数据库同步时如果出现 进程未能读取文件 因为发生操作系统错误 5。解决方法:
- Lucky Number
- linux命令parallel实现多进程并行计算
- linux 内核30题
- Wikioi 1985 GameZ游戏排名系统
- asp.net3.5网站发布到IIS选择4.0后默认首页无效
- linux内核学习笔记之——list_for_each_entry
- 数据库开发之【ADO.NET级别事务】