|
利用Windows系统自带的简繁体转换的代码,效率较高,能够很好的完成GB2312、GBK到BIG5的转换 主要解答者: taogx 提交人: taogx 感谢: taogx 审核者: flyelf 论坛对应贴子: 查看 A : 0 N$ E# Y) r# V' U3 E
include
0 ]3 P+ }9 N: Q c7 S, X( M#include
8 T6 v' H, \# z% c#include
# n$ C8 W2 ~5 U1 T5 ]! D5 x#include ! |( e" Z H, o4 o9 ?
#include
6 k+ d) @& n; ]2 \0 q; P6 e: i: H
9 {1 O- {6 Y$ f8 {char * j2f(char *s)3 }5 s4 y) Z ^! u* N3 o2 h Z
{
8 z! C: A+ o' M2 w/ u int n=strlen(s);/ C7 d$ r! M+ e: J
int r=LCMapString(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC),LCMAP_TRADITIONAL_CHINESE,s,strlen(s), NULL,0);2 B& N; S) y% n/ j8 p* n3 X' A6 R
if (!r)
! A+ @9 n" l$ x2 l/ K+ l6 j; l! A return NULL;
4 y, _' N$ t6 C) P char *ft=new char[r+1];
( ^% z8 d5 i' k r=LCMapString(MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_CHINESE_PRC),LCMAP_TRADITIONAL_CHINESE,s,strlen(s),ft,r+1);//这个api搞掂简体转繁体,下面会打?繁体出来
], i' e2 i; J2 Q if (r) {* n4 U9 Q4 E! f% w }7 s! g
ft[r]=0;1 ?7 R9 l, @- K/ ]9 N) A& B
wchar_t *pws=new wchar_t[r+1];4 m2 ]" t* L0 u$ ~' y+ X4 M# J; O
int r1=MultiByteToWideChar(936,0,ft,r,pws,r+1);0 e9 ~/ n! G+ V! k" `1 S3 S$ g
BOOL f=FALSE;% J+ G1 q4 K7 l) B
r1=WideCharToMultiByte(950,0,pws,r1,ft,r+1,"?",&f);//代码页切换搞掂gb->big5
3 j0 m. W6 |' B0 v ft[r1]=0;
& O m2 i, c* A/ w) v delete [] pws;
! {$ v$ A7 x8 h9 a$ k- i }
9 i u( ]% \: k- O! [ strncpy(s,ft strlen(s));
+ |2 l+ @2 |7 _1 D delete []ft;. W% o% Q9 q% {" _( i- L
return s;
; i1 G0 n' K' ^2 d4 s} 返回NULL表示失败,如果s为const型,需要提供输出缓冲区,否则你必须释放这个函数返回来的指针的空间。将 strncpy(s,ft,strlen(s)); return s; 这两句的改为缓冲区地址即可。 |
|