简体   繁体   中英

How to convert file from windows utf-16 or windows utf-8 to unix utf-16 with C++

Now, we use C++ on windows deal with some data. We have to convert some files to unix uft-16's xmls, the xml files are stored on a Unix server.

So I want to kown how to convert file from windows utf-16 or windows utf-8 to unix utf-16 using C++ ?

std::codecvt_utf8_utf16 which requires codecvt header can be used to convert UTF-8 to UTF-16. A Sample from this link . A compiler which supports C++11 is required.

// codecvt_utf8_utf16 example
#include <iostream>
#include <locale>
#include <string>
#include <codecvt>

int main ()
{
  std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> conversion;
  std::string mbs = conversion.to_bytes( u"\u4f60\u597d" );  // ni hao (你好)

  // print out hex value of each byte:
  std::cout << std::hex;
  for (int i=0; i<mbs.length(); ++i)
    std::cout << int(unsigned char(mbs[i])) << ' ';
  std::cout << '\n';

  return 0;
}

More information can be found in the above mentioned link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM