zoukankan      html  css  js  c++  java
  • UTF8

    Here's a couple of functions (based on Brian Bondy's example) that use WideCharToMultiByte and MultiByteToWideChar to convert between std::wstring and std::string using utf8 to not lose any data.

    // Convert a wide Unicode string to an UTF8 string 
    std::string utf8_encode(const std::wstring &wstr) 
    { 
        int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL); 
        std::string strTo( size_needed, 0 ); 
        WideCharToMultiByte                  (CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL); 
        return strTo; 
    } 
      
    // Convert an UTF8 string to a wide Unicode String 
    std::wstring utf8_decode(const std::string &str) 
    { 
        int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0); 
        std::wstring wstrTo( size_needed, 0 ); 
        MultiByteToWideChar                  (CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed); 
        return wstrTo; 
    } 
  • 相关阅读:
    第一次冲刺站立会议03
    第二次冲刺计划会议
    梦断代码阅读笔记02
    学习进度12
    个人项目——找水王
    学习进度11
    梦断代码阅读笔记01
    学习进度10
    学习进度09
    第一次冲刺个人博客10
  • 原文地址:https://www.cnblogs.com/hongjiumu/p/3525494.html
Copyright © 2011-2022 走看看