#include <locale.h>
#include <stdlib.h>
bool onvifBridge::Gbk2utf8(string &utfStr, string &srcStr)
{
if(NULL==setlocale(LC_ALL,"zh_CN.gbk"))
{
printf("Bad Parameter\n");
return false;
}
int unicodeLen=mbstowcs(NULL,srcStr.c_str(),0);
if(unicodeLen<=0)
{
printf("Can not Transfer!!!\n");
return false;
}
wchar_t *unicodeStr=(wchar_t *)calloc(sizeof(wchar_t),unicodeLen+1);
mbstowcs(unicodeStr,srcStr.c_str(),srcStr.size());
if(NULL==setlocale(LC_ALL,"zh_CN.utf8"))
{
printf("Bad Parameter\n");
return false;
}
int utfLen=wcstombs(NULL,unicodeStr,0);
if(utfLen<=0)
{
printf("Can not Transfer!!!\n");
return false;
}
char utfbuf[utfLen];
wcstombs(utfbuf,unicodeStr,utfLen);
utfbuf[utfLen]=0;
free(unicodeStr);
utfStr = utfbuf;
return true;
}
bool onvifBridge:: Utf82gbk(std::string &gbkStr, std::string &srcStr)
{
if(NULL==setlocale(LC_ALL,"zh_CN.utf8"))
{
printf("Bad Parameter\n");
return false;
}
int unicodeLen=mbstowcs(NULL,srcStr.c_str(),0);
if(unicodeLen<=0)
{
printf("Can not Transfer!!!\n");
return false;
}
wchar_t *unicodeStr=(wchar_t *)calloc(sizeof(wchar_t),unicodeLen+1);
mbstowcs(unicodeStr,srcStr.c_str(),srcStr.size());
if(NULL==setlocale(LC_ALL,"zh_CN.gbk"))
{
printf("Bad Parameter\n");
return false;
}
int gbkLen=wcstombs(NULL,unicodeStr,0);
if(gbkLen<=0)
{
printf("Can not Transfer!!!\n");
return false;
}
char gbkbuf[gbkLen];
wcstombs(gbkbuf,unicodeStr,gbkLen);
gbkbuf[gbkLen]=0;
gbkStr = gbkbuf;
free(unicodeStr);
return true;
}