Ejemplo n.º 1
0
void CCharsetConverter::utf16LEtoUTF8(const void *strSource,
                                      CStdStringA &strDest)
{
  if (m_iconvUtf16LEtoUtf8 == (iconv_t) - 1)
    m_iconvUtf16LEtoUtf8 = iconv_open("UTF-8", "UTF-16LE");

  if (m_iconvUtf16LEtoUtf8 != (iconv_t) - 1)
  {
    size_t inBytes = 2;
    uint16_t *s = (uint16_t *)strSource;
    while (*s != 0)
    { 
      s++;
      inBytes += 2;
    }
    // UTF-8 is up to 4 bytes/character, or up to twice the length of UTF-16
    size_t outBytes = inBytes * 2;

    const char *src = (const char *)strSource;
    char *dst = strDest.GetBuffer(outBytes);
    if (iconv_const(m_iconvUtf16LEtoUtf8, &src, &inBytes, &dst, &outBytes) ==
        (size_t)-1)
    { // failed :(
      strDest.clear();
      strDest.ReleaseBuffer();
      return;
    }
    strDest.ReleaseBuffer();
  }
}
Ejemplo n.º 2
0
CStdStringA localWideToUtf(LPCWSTR wstr)
{
  if (wstr == NULL)
    return "";
  int bufSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
  CStdStringA strA ("", bufSize);
  if ( bufSize == 0 || WideCharToMultiByte(CP_UTF8, 0, wstr, -1, strA.GetBuf(bufSize), bufSize, NULL, NULL) != bufSize )
    strA.clear();
  strA.RelBuf();
  return strA;
}