开发学院软件开发C语言 C语言实现GBK/GB2312/五大码之间的转换 阅读

C语言实现GBK/GB2312/五大码之间的转换

 2010-10-24 11:06:57 来源:WEB开发网   
核心提示:以下四个函数分别实现:大五码转GBK码/GBK转大五码GB2312码转GBK码/GBK码转GB2312码//---// 大五码转GBK码:void __fastcall BIG52GBK(char *szBuf){ if(!strcmp(szBuf, "")) return; int nStrLen

以下四个函数分别实现:

大五码转GBK码/GBK转大五码

GB2312码转GBK码/GBK码转GB2312码

//---------------------------------------------------------------------------
// 大五码转GBK码:
void __fastcall BIG52GBK(char *szBuf)
{
  if(!strcmp(szBuf, ""))
   return;
  int nStrLen = strlen(szBuf);
  wchar_t *pws = new wchar_t[nStrLen + 1];
  try
  {
   int nReturn = MultiByteToWideChar(950, 0, szBuf, nStrLen, pws, nStrLen + 1);
   BOOL bValue = false;
   nReturn = WideCharToMultiByte(936, 0, pws, nReturn, szBuf, nStrLen + 1, "?", &bValue);
   szBuf[nReturn] = 0;
  }
  __finally
  {
   delete[] pws;
  }
}
//---------------------------------------------------------------------------
// GBK转大五码
void __fastcall GBK2BIG5(char *szBuf)
{
  if(!strcmp(szBuf, ""))
   return ;
  int nStrLen = strlen(szBuf);
  wchar_t *pws = new wchar_t[nStrLen + 1];
  try
  {
   MultiByteToWideChar(936, 0, szBuf, nStrLen, pws, nStrLen + 1);
   BOOL bValue = false;
   WideCharToMultiByte(950, 0, pws, nStrLen, szBuf, nStrLen + 1, "?", &bValue);
   szBuf[nStrLen] = 0;
  }
  __finally
  {
   delete[] pws;
  }
}
//----------------------------------------------------------------------------
// GB2312码转GBK码
void __fastcall GB2GBK(char *szBuf)
{
  if(!strcmp(szBuf, ""))
   return;
  int nStrLen = strlen(szBuf);
  WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
  int nReturn = LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nStrLen, NULL, 0);
  if(!nReturn)
   return;
  char *pcBuf = new char[nReturn + 1];
  try
  {
   wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC);
   LCMapString(wLCID, LCMAP_TRADITIONAL_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
   strncpy(szBuf, pcBuf, nReturn);
  }
  __finally
  {
   delete[] pcBuf;
  }
}
//---------------------------------------------------------------------------
// GBK码转GB2312码
void __fastcall GBK2GB(char *szBuf)
{
  if(!strcmp(szBuf, ""))
   return;
  int nStrLen = strlen(szBuf);
  WORD wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
  int nReturn = LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nStrLen, NULL, 0);
  if(!nReturn)
   return;
  char *pcBuf = new char[nReturn + 1];
  try
  {
   wLCID = MAKELCID(MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_BIG5);
   LCMapString(wLCID, LCMAP_SIMPLIFIED_CHINESE, szBuf, nReturn, pcBuf, nReturn + 1);
   strncpy(szBuf, pcBuf, nReturn);
  }
  __finally
  {
   delete []pcBuf;
  }
}
//---------------------------------------------------------------------------
// 测试代码
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  char szBuf[255];
  // 从GB2312转到GBK
  strcpy(szBuf, Edit1->Text.c_str());
  GB2GBK(szBuf);
  Edit2->Text = String(szBuf);
  // 从GB2312转到BIG5,通过GBK中转
  strcpy(szBuf, Edit1->Text.c_str());
  GB2GBK(szBuf);
  GBK2BIG5(szBuf);
  Edit3->Text = String(szBuf);
}


 

转换编码集为gb2312或者gbk的方法

http://tech.cncms.com/shouji/iphone/47252.html

Tags:C语言 GBK GB2312

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接