Exemplo n.º 1
0
/* -----------------------------------------------------------------------
 *  関数名  :  CUnicodeF::sjis_to_utf8
 *  機能概要:  SJIS -> UTF-8 へ文字列のコード変換
 *  引数    :  (I)  char *pAnsiStr - 変換元SJIS文字列へのポインタ
 *              (O)  int *nBytesOut - 変換後文字列のバイト数
 *  戻り値  :  char* 変換後UTF-8文字列へのポインタ
 *
 *  処理概要:
 * -----------------------------------------------------------------------
 */
char *CUnicodeF::sjis_to_utf8(const char *pAnsiStr, int *nBytesOut)
{
    int nNum, nBytes;

    wchar_t *pwcWork = sjis_to_utf16be( pAnsiStr, &nNum);
    char *pcUtf8 = utf16be_to_utf8( pwcWork, &nBytes);
    free( pwcWork);

    *nBytesOut = nBytes;
    return pcUtf8;
}
Exemplo n.º 2
0
	void WinCodepageConverter::_bufferToUnicode<1>(StringHolder<1> _destString, const ByteBuffer& _srcBuffer)
	{
		// An empty buffer needs special processing.
		if(_srcBuffer.empty())
			return;

		const uint8* data;
		size_t sz;
		Utf8Buffer  buf8;

		switch(getCodepage())
		{
			case CP_UTF8:
			{
				// UTF-8 -> UTF-8
				data = _srcBuffer.data();
				sz = _srcBuffer.size();
				break;
			}
			case CP_UTF16LE:
			{
				// UTF-16LE -> UTF-8
				utf16le_to_utf8(buf8, _srcBuffer.data(), _srcBuffer.size());
				data = buf8.data();
				sz = buf8.size();
				break;
			}
			case CP_UTF16BE:
			{
				// UTF-16BE -> UTF-8
				utf16be_to_utf8(buf8, _srcBuffer.data(), _srcBuffer.size());
				data = buf8.data();
				sz = buf8.size();
				break;
			}
			case CP_UTF32LE:
			{
				// UTF-32LE -> UTF-8
				utf32le_to_utf8(buf8, _srcBuffer.data(), _srcBuffer.size());
				data = buf8.data();
				sz = buf8.size();
				break;
			}
			case CP_UTF32BE:
			{
				// UTF-32BE -> UTF-8
				utf32be_to_utf8(buf8, _srcBuffer.data(), _srcBuffer.size());
				data = buf8.data();
				sz = buf8.size();
				break;
			}
			default:
			{
				// source code page -> UTF-16
				size_t maxsz = _srcBuffer.size() * 2;
				Utf16Buffer buf16(maxsz);
				
				int r = MultiByteToWideChar(getCodepage(), 0, 
					char_cast<LPCSTR>(_srcBuffer.data()), _srcBuffer.size(), 
					char_cast<LPWSTR>(buf16.data()), buf16.size());
				if(r == 0)
				{
					UnicodeSupport::_throwCouldNotConvert("UTF-16", getName(), _srcBuffer.data(), _srcBuffer.size());
					return;
				}

				// UTF-16 -> UTF-8
				utf16_to_utf8_unsafe(buf8, buf16.data(), r);
				data = buf8.data();
				sz = buf8.size();
			}
		}
		_destString.append(data, sz);
	}