zoukankan      html  css  js  c++  java
  • 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":"u4e2du56fd","province":"u5c71u4e1c","city":"u6f4du574a","district":"","isp":"u7535u4fe1","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:[u5c71u4e1c]
    //us:[山东]
    //
    




    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    智能Office同步器Alpha 1(界面预览)
    SQLite系列免费/开源数据库组件/应用
    软件推荐:智能PE资源提取器
    怎样编写MS Office安全插件
    博客园Logo创意之我的朋友弄的
    DNN(DotNetNuke)研究手札系列1-资源
    VB5/6反编译器(半)
    关于Peer Review、代码评审和测试驱动等
    [转载]关于怎样优化HTML以加快下载速度的10个方法
    完全优化MySQL数据库性能的八大巧方法
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834851.html
Copyright © 2011-2022 走看看