Sunday, February 25, 2007
在BBS上看到大二時寫的舊文
五年前的文章,真的很熱血。
作者: fruitfox (rock,baby !) 站內: P-fruitfox標題: [轉錄][通告] 給大家的一封信時間: 2005/08/04 Thu 15:11:36
※ 本文轉錄自 [fruitfox] 信箱
作者: fruitfox ()標題: [通告] 給大家的一封信時間: Thu Jun 6 00:35:19 2002
※ [通告] 共 3 人收件※ KETU BraveHeart mars93---------------------------------------------------------------------------
恩,那就單刀直入吧
各位覺得這次遊戲設計比賽能挑贏學長那一隊的機率是多少??
我自己估了一下 [1m以目前的情勢來看[1m[0m 是沒有小於萬分之一
不過也沒有大於千分之一啦
以目前的實力,要贏過學長是一件機率很小的事情,我是了然於胸的
更何況,我們是第93隊,強敵環伺,高手不知凡幾
既然如此,為甚麼我還邀各位來送死?
我想知道 要是我整個暑假投入一件工作
半年之後 我可以交出怎樣的成績單
因為我不想要一個
每天睡到日上三竿 手握遙控器換百台的日子
因為我想要嘗試長時間
沒日沒夜的吸收 以及付出
最重要的是 因為我想知道
我們大家齊力 可以走多遠
想要知道這些事情 是要有決心以及覺悟的
各位 準備好了嗎 ?
--
"And most imporant,because you are alive."
--[1;32m※ Origin: [33m成功大學資訊工程學系[醉資心BBS] [37m [31m◆ From: [36m140.116.142.47[m [1;34m⊕ Modify: [1;30mfruitfox[m
Labels:
Life
Friday, February 9, 2007
Unicode使用123(續)
因為上官大大的發問,我一時好奇就做了幾個實驗。
因為想說的有點多,所以另外開一篇文章。
關於使用"Unicode"中文,就用_T("這是中文")。
下面是我的實驗心得
(under Windows XP SP2, Visual Stdio 2005)
1.使用printf
這是一個很詭異的function
如果把string轉換成multibyte,printf不能正常印出
但是!!如果直接餵中文給他吃,卻可以正常的印出。
推測是因為呢 _T把bit order給重排了
而printf裡面的運作是以char為單位
所以呢...造成了這樣的現象
如果說用命令列引數傳給printf印也是正常印出
我懷疑在這過程中我給的中文引數被動了手腳...
恩恩 以上純屬推測XD
2.關於wprintf
原來C裡面的multibyte不一定是Unicode,有可能是任何的編碼方式
如果直接餵他吃multibyte的中文...也是不行印出來
但是如果先呼叫setlocale(LC_ALL,"chs");
就可以讓wprintf知道要印出來的是中文,就可以了。
MSDN上面關於setlocale的資訊:Use the setlocale function to set, change, or query some or all of the current program locale information specified by locale and category.
3.關於_tprintf
printf與wprintf的組合
4.關於WriteConsole
Windows NT之後使用的是Unicode編碼
其API也對Unicode有良好支援
如果要輸出純Unicode String
這裡是一個例子
TCHAR CHString[100] = _T("這是中文!\n");
DWORD ws;
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
CHString,
wcslen(CHString),
&ws,
NULL);
如果有define unicode的話,就會呼叫WriteConsoleW,不然的話就會呼叫WriteConsoleA
來處理ASCII。
5.心得
C lib支援multibyte的編碼,但不一定是Unicode。
使用setlocale(LC_ALL,"chs");設定編碼方式
可以讓multibyte的C function印出中文
也就是說要寫general的程式 _tprintf可以辦的到
但是要先setlocale
但是!
如果需要完整Unicode的支援,最好使用Windows所提供的API。
而且Windows API大都實做成general的方式
會看處理的字串是Unicode或是ASCII作對應的轉換
所以guidline要加上一條...Windows底下請愛用Windows API
如果很在意可攜性,那麼進一步研究C lib 的multibyte是必要的...
(在下目前是Windows guy 既然NT與CE都support Unicode...就讓我偷懶一下吧:P )
reference:
http://blog.vckbase.com/bruceteen/archive/2005/11/15/14924.aspx
http://www.cppblog.com/sandy/archive/2006/06/21/8779.html
http://blog.csdn.net/imwkj/archive/2007/01/23/1490647.aspx
Labels:
Programming
Thursday, February 8, 2007
Unicode使用123
記得碩一的時候為了處理中文路徑就已經survey過了
但是年代久遠又忘光了,這次就做個筆記吧!
(from Windows System Programming 3th)
Windows NT之後內部的系統都是用unicode在作處理(豆知識)
NTFS的file name與file path都是unicode編碼
在Windows 底下如果要寫可以同時處理Unicode(UTF-16)與ASCII(8-bits)字串的程式,
以下有 幾條guideline可以follow:
1.使用TCHAR、LPTSTR以及LPCTSTR
我們來看一下TCHAR的定義:
#ifdef UNICODE
#define TCHAR WCHAR
#else
#define TCHAR CHAR
#endif
很棒吧!
LPTSTR則是:
#ifdef UNICODE
typedef LPWSTR LPTSTR;
#else
typedef LPSTR LPTSTR;
#endif
LPCTSTR:
#ifdef UNICODE
typedef LPCWSTR LPCTSTR;
#else
typedef LPCSTR LPCTSTR;
#endif
簡單的說,上面的三個type會依照程式的define,在Unicode或ASCII之間變換
2.如果要使用Unicode,在所有的module中定義#define UNICODE
以及 #define _UNICODE。前者控制windows function,後者控制C library。
這兩個定義要下在windows.h之前。如果要處理的是ASCII的話,就不需要define。
3.用sizeof(TCHAR)幫你計算buffer size
4.使用tchar.h中的general C library function,如:
_fgettc, _itot (for itoa), _stprintf (for sprintf), _tstcpy (for strcpy), _ttoi,
_totupper, _totlower,_tprintf.
"_"代表的意義是這些function由Microsoft C所提供,這些function在其他的系統上
應該是以不同的名字出現。
5.include tchar.h after windows.h,to get required definitions for text macros and
generic C library functions
6.用下面的格式表示constant string:
L"this is string"
_T("this is string")
前者由ANSI C所提供,後者由Microsoft C compiler所支援。
書中還有提到幾個好玩的事情
1.String comparisons can use lstrcmp and lstrcmpi rather than the generic _tcscmp and _tcscmpi to account for the specific language and region, or locale, at run time and also to perform word rather than string comparisons.String comparisons simply compare the numerical values of the characters, whereas word comparisons consider locale-specific word order.
有意思,下次來用看看會得到怎樣的結果。
2.程式的entry point可以用_tmain來取代,會依照定義變成main或wmain。
#include
#include
int _tmain (int argc, LPTSTR argv [])
{
...
}
這樣就可以吃ASCII或Unicode的命令列引數了
3.有一些程式會依照define在底層做變形,例如CreateFile,當定義Unicode時會呼叫
CreateFileW,不然就會呼叫CreateFileA。在Debug資訊裡面就會看到這些我們沒有呼叫的
function。(奇怪,我明明呼叫的是CreateFile,怎麼有CreateFileA??)
Labels:
Programming
Subscribe to:
Posts (Atom)