ASCIIコード回転UTF-8コード

1574 ワード

#include "stdafx.h"

#include "StringBuffer.h"



CString Unicode2Ascii(WCHAR *in_str,int length)

{

    BYTE* psz = (BYTE*)in_str;

	char temp1_str[4], temp2_str[2];

	char w_temp;

	CString dest_str, str;

	temp1_str[0] = '&';

	temp1_str[1] = '#';

	temp1_str[2] = 'x';

	temp1_str[3] = '/0';

	temp2_str[0] = ';';

	temp2_str[1] = '/0';

	for(int i=0; i<2*length;i++)

	{

		if(i%2 != 0)

		{

			w_temp = psz[i];

			psz[i] = psz[i-1];

			psz[i-1] = w_temp;

			dest_str += temp1_str;

			str.Format( "%x", psz[i-1] );

			if(str.GetLength()<2)

				str = "0" + str;

			dest_str += str;

			str.Format( "%x", psz[i] );

			if(str.GetLength()<2)

				str = "0" + str;

			dest_str += str;

			

			dest_str += temp2_str;

		}

	}

	return dest_str;

}



void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)

{

	::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);

	return;

}



CString GbToUnicodeAscii(char *pText)

{	

	CStringBuffer stringBuffer(4096);

	int i = 0;

	int iLen = strlen(pText);

	while(i < iLen)

    {

        // 

        if( *(pText + i) >= 0)

        {

            //rst[j++] = pText[i++];

			stringBuffer.Append(pText[i++]);

        }

        else

        {

			WCHAR pbuffer;

			Gb2312ToUnicode(&pbuffer,pText+i);

			CString str_ascii = Unicode2Ascii(&pbuffer,1);

			stringBuffer.Append(str_ascii);

			i += 2;

        }

	}

    

    return stringBuffer.ToString();

	

}