CallmeSunnyday / opencc

Automatically exported from code.google.com/p/opencc
0 stars 0 forks source link

encoding.c 亂碼問題 #6

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What is the problem? How to reproduce the problem?
有何問題?如何重現問題?

亂碼問題:

opencc.c
---------
char * opencc_convert_utf8(opencc_t t_opencc, const char * inbuf, size_t length)
{
    if (!lib_initialized)
        lib_initialize();

    if (length == (size_t) -1 || length > strlen(inbuf))
        length = strlen(inbuf);

    /* 將輸入數據轉換爲ucs4_t字符串 */
    ucs4_t * winbuf = utf8_to_ucs4(inbuf, length);

    /* 測試轉換是否成功
    return ucs4_to_utf8(winbuf, (size_t) -1);

    /* 結果是亂碼,就算 inbut 是 "A",轉換後的 winbuf 都是亂碼 */
}

/* ver 0.1.0 的 opencc 沒有這個問題,問題在於 encoding.c */
encoding.c
----------
ucs4_t * utf8_to_ucs4(const char * inbuf, size_t inbuf_len);
char * ucs4_to_utf8(const ucs4_t * inbuf, size_t inbuf_len);

0.2.0 換上 0.1.0 的 encoding.h & encoding.c,可解決問題

What version of the product are you using? On what operating system?
您在用什麼版本?在什麼平臺下?

Win32 & Linux

Please provide any additional information below.
請把附加信息寫在下面。

input & output 是 file 沒有這問題,問題發生於是用 char* 
是由其他 program 輸入

p.s. 我有用 opencc 透過 JNI 作為 Jave based web app 繁簡轉換的 
engine。之前用 0.0.5,近來打算升級 0.2.0 
時發現有問題,後來退 0.1.0 
就沒有問題,但未有時間深入研究。

但近來,我正在用 Qt 編寫 opencc 旳 GUI, 用 debugger 
發現了這問題。

另外,未知 Qt 寫 GUI 是否乎合板主的意願,我真的想發展 
opencc,板主加油

Original issue reported on code.google.com by copperox...@gmail.com on 25 Jun 2011 at 12:39

GoogleCodeExporter commented 8 years ago
請你具體說一下錯誤是如何產生的,input是file沒有問題,char 
*輸入就有問題?這個錯誤我無法重現。

0.2.0最大的改動就是取消了對iconv的依賴,重寫了encoding.c。

非常高興你可以用qt寫opencc的GUI,現在opencc缺乏一個gui。

Original comment by byvo...@gmail.com on 25 Jun 2011 at 1:30

GoogleCodeExporter commented 8 years ago
re-produce 方法如下

Create a new file, let say, hello.c with UTF-8 encoding

hello.c (UTF-8 encoding)
--------------
#include <stdio.h>
#include <stdlib.h>
#include "encoding.h"

int main(void) {
    const char* text = "測試";
    printf("%s\n", text);
    printf("%s\n", ucs4_to_utf8(utf8_to_ucs4(text, -1), -1));
    return 0;
}

the console will output 亂碼. However, replace enconding.h and encoding.c 
with version 0.1.0, it will output normally.

Original comment by copperox...@gmail.com on 26 Jun 2011 at 3:08

GoogleCodeExporter commented 8 years ago

Original comment by byvo...@gmail.com on 9 Aug 2011 at 8:58