#ifndef COMMON_H
#define COMMON_H
#include <string>
#include <tchar.h>
#include <windows.h>
#pragma execution_character_set("utf-8")
using namespace std;
class Common
{
public:
Common();
static void UTF_8ToUnicode(WCHAR* pOut,char *pText);
static void UnicodeToGB2312(char* pOut,WCHAR uData);
static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);
};
#endif
#include "common.h"
Common::Common()
{
}
void Common::UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);
return;
}
void Common::UnicodeToGB2312(char* pOut,WCHAR uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
return;
}
void Common::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char buf[4];
char* rst = new char[pLen + (pLen >> 2) + 2];
memset(buf,0,4);
memset(rst,0,pLen + (pLen >> 2) + 2);
int i =0;
int j = 0;
while(i < pLen)
{
if(*(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(buf,Wtemp);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
i += 3;
j += 2;
}
}
rst[j]='\0';
pOut = rst;
delete []rst;
}
string examName = ui->laName->text().toStdString();
QTextCodec *code = QTextCodec::codecForName("GB2312");
string selectedFile = code->fromUnicode(username.c_str()).data();