用iconv库对文本进行编码处理

#include <iconv.h>

#include <string.h>

#include <locale.h>
int main(int argc, char* argv[])

{

  setlocale( LC_ALL, "" );
  char* pTestText = "GB2312编码转换到UNICODE编码";

  size_t inbytesleft = strlen(pTestText) + 1;

  size_t outbytesleft = inbytesleft * 2;

  wchar_t* pConvertedText = new wchar_t[inbytesleft];
  const char* inbuf = pTestText;

  char* outbuf = (char*)pConvertedText;
  iconv_t cd = iconv_open( "ucs-2-internal", "gb2312" );

  size_t error = iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft );

  iconv_close( cd );
  if( pConvertedText )

  {

    wprintf( L"%s\n", pConvertedText );

    delete[] pConvertedText;

  }
  return 0;

}

你可能感兴趣的:(iconv)