简体转繁体的问题(gb2312 to big5)

111222 2005-08-21 05:50:26
下面这段代码为什么转换后得到的很多字符只能显示成 '?'

比如保卫的'卫'.

void CTranslation::Gb2Big(CString &strReturned)
{
if(IsTraditrionSystem())//判断用户windows是否为繁体
{
const char* pGbJtAsiic = strReturned;

int nGbJtAsiicLen = strlen(pGbJtAsiic);

char *pGbFtAsiic = new char[nGbJtAsiicLen+1];
memset(pGbFtAsiic,0,nGbJtAsiicLen+1);

LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE,pGbJtAsiic,nGbJtAsiicLen,pGbFtAsiic,nGbJtAsiicLen+1);

int nGbUnicodeLen = MultiByteToWideChar(936,0,pGbFtAsiic,-1,NULL,0);

wchar_t *pGbUnicode = new wchar_t[nGbUnicodeLen+1];

memset(pGbUnicode,0,sizeof(wchar_t)*(nGbUnicodeLen+1));

MultiByteToWideChar(936,0,pGbFtAsiic,nGbJtAsiicLen,pGbUnicode,nGbUnicodeLen+1);

int nBig5AsiicLen = WideCharToMultiByte(950,0,pGbUnicode,-1,NULL,0,NULL,NULL);

char* pBig5Asiic = new char[nBig5AsiicLen +1];

WideCharToMultiByte(950,0,pGbUnicode,nGbUnicodeLen,pBig5Asiic,nBig5AsiicLen,NULL,NULL);

delete[] pGbUnicode;

delete[] pGbFtAsiic;

strReturned = pBig5Asiic;

delete [] pBig5Asiic;
}
}
...全文
466 点赞 收藏 8
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zdleek 2006-01-10
GB转到BIG5在多数程序里会显示为乱码。
如果在繁体操作系统下,他就会正常显示。
如果在简体的操作系统中,需要显示的程序能够自动识别并选用big5编码来显示
例如,IE你就可以选择语言的编码是GB或BIG5
但是程序里的EDIT控件的则不会选择编码,他们默认使用操作系统默认的编码
回复
lixiaosan 2006-01-10
用API 作简繁体转换



1. 输入Big5字符,返回Gb简体字符

//---------------------------------------------------------------------------

//函数输入Big5字符,返回Gb简体字符

//---------------------------------------------------------------------------

AnsiString __fastcall Big2Gb(AnsiString sBig)

{

char* pszBig5=NULL; //Big5编码的字符

wchar_t* wszUnicode=NULL; //Unicode编码的字符

char* pszGbt=NULL; //Gb编码的繁体字符

char* pszGbs=NULL; //Gb编码的简体字符

AnsiString sGb; //返回的字符串

int iLen=0; //需要转换的字符数



pszBig5=sBig.c_str(); //读入需要转换的字符参数



//计算转换的字符数

iLen=MultiByteToWideChar (950, 0, pszBig5, -1, NULL,0) ;

//给wszUnicode分配内存

wszUnicode=new wchar_t[iLen+1];

//转换Big5码到Unicode码,使用了API函数MultiByteToWideChar

MultiByteToWideChar (950, 0, pszBig5, -1, wszUnicode,iLen);



//计算转换的字符数

iLen=WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ;

//给pszGbt分配内存

pszGbt=new char[iLen+1];

//给pszGbs分配内存

pszGbs=new char[iLen+1];

//转换Unicode码到Gb码繁体,使用API函数WideCharToMultiByte

WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, pszGbt,iLen, NULL, NULL) ;



//转换Gb码繁体到Gb码简体,使用API函数LCMapString

LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE, pszGbt, -1, pszGbs, iLen);



//返回Gb码简体字符

sGb=pszGbs;



//释放内存

delete [] wszUnicode;

delete [] pszGbt;

delete [] pszGbs;



return sGb;

}

2. 输入Gb字符,返回Big5字符

//---------------------------------------------------------------------------

//函数输入Gb字符,返回Big5字符

//---------------------------------------------------------------------------

AnsiString __fastcall Gb2Big(AnsiString sGb)

{

char* pszGbt=NULL; //Gb编码的繁体字符

char* pszGbs=NULL; //Gb编码的简体字符

wchar_t* wszUnicode=NULL; //Unicode编码的字符

char* pszBig5=NULL; //Big5编码的字符

AnsiString sBig5; //返回的字符串

int iLen=0; //需要转换的字符数



pszGbs=sGb.c_str(); //读入需要转换的字符参数



//计算转换的字符数

iLen=MultiByteToWideChar (936, 0, pszGbs, -1, NULL,0) ;



//给pszGbt分配内存

pszGbt=new char[iLen*2+1];

//转换Gb码简体到Gb码繁体,使用API函数LCMapString

LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, iLen*2);



//给wszUnicode分配内存

wszUnicode=new wchar_t[iLen+1];

//转换Gb码到Unicode码,使用了API函数MultiByteToWideChar

MultiByteToWideChar (936, 0, pszGbt, -1, wszUnicode,iLen);



//计算转换的字符数

iLen=WideCharToMultiByte (950, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ;

//给pszBig5分配内存

pszBig5=new char[iLen+1];

//转换Unicode码到Big5码,使用API函数WideCharToMultiByte

WideCharToMultiByte (950, 0, (PWSTR) wszUnicode, -1, pszBig5,iLen, NULL, NULL) ;



//返回Big5码字符

sBig5=pszBig5;



//释放内存

delete [] wszUnicode;

delete [] pszGbt;

delete [] pszBig5;



return sBig5;

}





回复
luolovegui 2006-01-10
学习.
回复
郁寒 2006-01-10
不知道为什么,路上的代码变在的乱字符,不知是不是程序语言设定的问题
回复
lixiaosan 2005-08-21
CString Gb2Big(char* sGb)
{
char* pszGbt = NULL;
char* pszGbs = NULL;
wchar_t* wszUnicode = NULL;
char* pszBig5 = NULL;
CString sBig5;
int iLen=0;

pszGbs = sGb;

iLen = MultiByteToWideChar(936, 0, pszGbs, -1, NULL,0);

pszGbt = new char[iLen*2+1];

LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, iLen*2);


wszUnicode = new wchar_t[iLen+1];

MultiByteToWideChar (936, 0, pszGbt, -1, wszUnicode,iLen);


iLen = WideCharToMultiByte (950, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL);

pszBig5 = new char[iLen+1];

WideCharToMultiByte (950, 0, (PWSTR)wszUnicode, -1, pszBig5, iLen, NULL, NULL);


sBig5 = pszBig5;


delete [] wszUnicode;
wszUnicode = NULL;
delete [] pszGbt;
pszGbt = NULL;
delete [] pszBig5;
pszBig5 = NULL;

return sBig5;
}

==========================
m_edit1.SetWindowText(Gb2Big("卫"));
回复
lixiaosan 2005-08-21
简体转繁体的问题(gb2312 to big5)

GB2312 也包含简体和繁体

big5是big5

不知道你是要BIG5的乱码还是gb2312的繁体。。
回复
lixiaosan 2005-08-21
char* Gb2Big(char* sGb)
{
char* pszGbt=NULL;
char* pszGbs=NULL;
wchar_t* wszUnicode=NULL;
char* pszBig5=NULL;
char* sBig5;
int iLen=0;

pszGbs = sGb;
iLen = MultiByteToWideChar(936, 0, pszGbs, -1, NULL,0);

pszGbt=new char[iLen*2+1];
LCMapString(0x0804,LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, iLen*2);

wszUnicode = new wchar_t[iLen+1];
MultiByteToWideChar (936, 0, pszGbt, -1, wszUnicode,iLen);

iLen=WideCharToMultiByte (950, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL); pszBig5 = new char[iLen+1];

WideCharToMultiByte (950, 0, (PWSTR)wszUnicode, -1, pszBig5, iLen, NULL, NULL);

sBig5=pszBig5;

delete [] wszUnicode;
wszUnicode = NULL;
delete [] pszGbt;
pszGbt = NULL;
delete [] pszBig5;
pszBig5 = NULL;

return sBig5;
}
回复
手插兜兜 2005-08-21
可能你已经转好了,但程序项目的语言属性你仍然设置为GB2312,没有设置为BIG5
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告