漢字簡體轉正體類及測試程式

2021-04-20 05:41:25 字數 4055 閱讀 4678

做了乙個將簡體漢字轉化成正體漢字的類chntrans,轉化賴字典檔案。

其中用open函式開啟字典檔案(有file *版本和cfile版本);用transchar轉化乙個漢字;用

trans轉化乙個字串。

chntrans.h

檔案內容:

#pragma once

#define unicode_file_flag 0xfeff

#define max_unicode_file_size 65535

class cchntrans

;chntrans.cpp

檔案內容:

#include "stdafx.h"

#include "chntrans.h"

cchntrans::cchntrans(void)

cchntrans::~cchntrans(void)

#if 0

//開啟檔案函式,cfile版本

bool cchntrans::open(const cstring * const pfilename)

cfile fochinesemap;

if(true != fochinesemap.open(*pfilename, cfile::moderead | cfile::sharedenywrite, null))

if(false == isunicodefile(&fochinesemap))

if(max_unicode_file_size < fochinesemap.getlength())

//檢測統一編碼字元檔案減去檔案頭的統一編碼標誌後的位元組數能否被tchar整除,不能則表示檔案有問題

if(0 !=(fochinesemap.getlength() - sizeof(tchar)) % sizeof(tchar))

uibuffertcharcount = (uint)((fochinesemap.getlength() - sizeof(tchar)) / sizeof(tchar));

pbuffer = (tchar *)malloc(uibuffertcharcount * sizeof(tchar));

if(null == pbuffer)

memset(pbuffer, 0, uibuffertcharcount * sizeof(tchar));

if((uibuffertcharcount * sizeof(tchar)) != fochinesemap.read(pbuffer, uibuffertcharcount * sizeof(tchar)))

fochinesemap.close();

return true;

}#endif

//開啟檔案函式,file *版本

bool cchntrans::open(const cstring * const pfilename)

file * pchinesemap = null;

pchinesemap = _wfopen(*pfilename, _t("rb"));

if(null == pchinesemap)

if(false == isunicodefile(pchinesemap))

/開始:檢測字典檔案的大小,字典檔案不得超過max_unicode_file_size + 2位元組,大於該大小按照該大小處理///

byte * pbuffertmp = (byte *)malloc(max_unicode_file_size);

if(null == pbuffertmp)

uint uifilesize = (uint)fread(pbuffertmp, sizeof(byte), max_unicode_file_size, pchinesemap);

if(0 != uifilesize % sizeof(tchar))

uibuffertcharcount = uifilesize / sizeof(tchar);

/結束:檢測字典檔案的大小,字典檔案不得超過max_unicode_file_size + 2位元組,大於該大小按照該大小處理///

pbuffer = (tchar *)malloc(uibuffertcharcount * sizeof(tchar));

if(null == pbuffer)

memset(pbuffer, 0, uibuffertcharcount * sizeof(tchar));

memcpy(pbuffer, pbuffertmp, sizeof(tchar) * uibuffertcharcount);

free(pbuffertmp);

fclose(pchinesemap);

return true;

}#if 0

//檢測輸入檔案是否非統一編碼(unicode)檔案,cfile版本

bool cchntrans::isunicodefile(cfile * const pofile)

tchar firsttchar = 0;

pofile->read(&firsttchar, sizeof(tchar));

if(unicode_file_flag != *pfirsttchar)

return true;

}#endif

//檢測輸入檔案是否非統一編碼(unicode)檔案,file *版本

bool cchntrans::isunicodefile(file * pfile)

tchar firsttchar = 0;

if((1 != fread(&firsttchar, sizeof(tchar), 1, pfile)) || (unicode_file_flag != firsttchar))

return true;

}//轉換單個漢字函式

bool cchntrans::transchar(tchar * const ptch)

uint i = 0;

for(; i < uibuffertcharcount; i += 3)

}return true;

}//轉換字串函式

bool cchntrans::trans(cstring * const pstrline)

if(0 == pstrline->getlength())

tchar * ptr = pstrline->getbuffer();

int ilinetcharnum = pstrline->getlength();

int i = 0;

for(; i < ilinetcharnum; i++)

ptr++;

}pstrline->releasebuffer();

return true;

}測試程式程式碼:

han.cpp

檔案內容:

// han.cpp : 定義主控台應用程式的進入點。

// #include "stdafx.h"

#include "han.h"

#include "chntrans.h"

#ifdef _debug

#define new debug_new

#endif

// 僅有的乙個應用程式物件

using

namespace std;

int _tmain(int argc, tchar* argv, tchar* envp)

class cchntrans * ptc = new cchntrans; 

cstring strfilename = _t("c://han.txt");

if(true != ptc->open(&strfilename))

cstring p = _t("一國家漢漢字國");

ptc->trans(&p);

delete ptc;

return nretcode;}

字型檔檔案

c:/han.txt

格式這樣寫(用記事本儲存成unicode形式):

乙個簡體漢字緊跟它的正體漢字空格乙個簡體漢字緊跟它的正體漢字空格……

如:漢漢 國國

漢字簡體繁體轉換 Javascript

最近看到有個簡體 繁體字互相轉換的程式,是用js實現的,感覺很好玩,所以拿來研究研究。先看看介面如下 漢字簡體繁體轉換 上面的所有 如下 script function copy ob function paste ob function cut ob function findobj n,d if...

C 漢字轉拼音 拼音轉漢字 簡體繁體互轉

之前一直想弄個漢字轉化工具,一直沒能實現,今天發現原來早就可以實現了,先給各位推薦下這個庫 這兩個提供這是微軟亞洲漢字開發中心開發的,下面是說明 east asia numeric formatting library 支援將小寫的數字字串格式化成簡體中文,正體中文,日文和韓文的大寫數字字串。jap...

Python中漢字繁簡體互轉

第一種方法 文件 安裝 pip install hanziconv 說明 可以進行繁簡體互轉 也可以比較繁簡體文字是否相同。from hanziconv import hanziconv line jian 123asdasd把中文字串進行繁體和簡體間的轉換 line fan 123asdasd把中...