小弟最近遇到oracle字符集的问题,两个数据库的字符集不同导致中文乱码问题,
java里面是通过在读写数据库中文字段时进行转码解决的.
Win32 API有两个函数WideCharToMultiByte 和 MultiByteToWideChar,先将ISO-8859-1转为Unicode,再转为GB2312,可是调试时,并没有转换成功,ISO-8859-1编码的字符串还是没变。
请教各位大虾?
代码如下:
const int ISO_8859_1 = 28591;
const int GB2312 = 936;

char *input = GetISO_8859_1_Data();

// Get size for the temporary buffer
int wideCharCount = MultiByteToWideChar(ISO_8859_1, 0, input, -1, NULL, 0);
if( wideCharCount == 0 )
{
   TRACE("Could not convert to unicode.");
   return FALSE;
}
// Do the actual conversion 
wchar_t *temp = new wchar_t[wideCharCount];
wideCharCount = MultiByteToWideChar(ISO_8859_1, 0, input, -1, temp, wideCharCount);
if( wideCharCount == 0 )
{
         delete[] temp;
TRACE("Could not convert to unicode.");
return FALSE;
}

// Get size for the output buffer 
int resultCount = WideCharToMultiByte(GB2312, 0, temp, wideCharCount, NULL, 0, NULL, NULL);
if( resultCount == 0 )
{
delete[] temp;
TRACE("Could not convert to destination codepage.");
return FALSE;
}

// Convert 
char *result = new char[resultCount];
resultCount = WideCharToMultiByte(GB2312, 0, temp, wideCharCount, result, resultCount, NULL, NULL);
delete[] temp;
if( resultCount == 0 )
{
delete[] result;
TRACE("Could not convert to destination codepage.");
return FALSE;
}

// Do something with the resultdelete[] result;
return TRUE;

解决方案 »

  1.   

    lfchen(一条晚起的虫) 
      没找到ISO_8859_1编码集。不好意思,小弟初次接触编码,不太明白
      

  2.   

    你的方法好像没问题的。
    最多 new的时候长度再加一。
      

  3.   

    看来我又能骗分了,呵呵。贴一段我的程序里的代码。用来把日文内码转换为GB,转换结果显示正常的日文
    希望对你有帮助。int Sjis2GB(CString &strSjis, CString &strGB)
    {
    wchar_t* wszUnicode=NULL; //Unicode编码的字符
    char* szBefore=NULL;
    char* szAfter=NULL;
    char* szResult=NULL;
    int nLen=0; //需要转换的字符数nLen=strSjis.GetLength();
    szBefore=new char[nLen+1];
    _tcscpy(szBefore, strSjis);//计算转换的字符数
    nLen=MultiByteToWideChar (932, 0, szBefore, -1, NULL,0) ;
    //给wszUnicode分配内存
    wszUnicode=new wchar_t[nLen+1];
    //转换Sjis码到Unicode码
    MultiByteToWideChar (932, 0, szBefore, -1, wszUnicode,nLen);//计算转换的字符数
    nLen=WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, NULL,0, NULL, NULL) ;
    //给szAfter分配内存
    szAfter=new char[nLen+1];
    //给szResult分配内存
    szResult=new char[nLen+1];
    //转换Unicode码到Gb码繁体
    WideCharToMultiByte (936, 0, (PWSTR) wszUnicode, -1, szAfter,nLen, NULL, NULL) ;//转换Gb码繁体到Gb码简体
    LCMapString(0x0804,LCMAP_SIMPLIFIED_CHINESE, szAfter, -1, szResult, nLen);strGB=szResult;//释放内存
    delete [] wszUnicode;
    delete [] szBefore;
    delete [] szAfter;
    delete [] szResult;return 0;
    }
      

  4.   

    我反过来将GB2312转换为ISO_8859_1,调试的时候怎么全是问号?
      

  5.   

    lfchen(一条晚起的虫) 是不是将数据库字段中的字符串读出来存放在char *,然后进行转换就可以了?
      

  6.   

    lfchen(一条晚起的虫)郁闷!试过了,还是不行。
    我读取得是US7ASCII字符集oracle数据库中的数据,然后要保存到ZHS16GBK字符集oracle数据库中?