Val編程-讀取漢字
? ? ?Val編程中,對于漢字的讀取不是很友好,利用fileget直接讀取記事本產生的文件字符串會導致亂碼的產生。因為Val只支持使用utf-8進行編碼,因此讀取的文本需要進行utf-8格式轉換。
? ? ?在GBK中,漢字占兩個字節。并且每個字節都大于128.可以通過直接讀取位來進行轉換來讀取漢字。通過讀取的兩個數字來獲得漢字的ASCII碼。
? ? ?而在utf-8中具有自己的編碼方式。模擬器中讀取的是用GBK編碼,而顯示的是UTF-8.
?
? ? ? ? ? ? ? ? ? ? ? ??
utf-8格式說明:
? ? 這是為傳輸而設計的編碼,其系列還有UTF-7和UTF-16
? ? 其中UTF-16和Unicode編碼大致一樣, UTF-8就是以8位為單元對Unicode進行編碼。從Unicode到UTF-8的編碼方式如下:
Unicode編碼(16進制) UTF-8 字節流(二進制)?
0000 - 007F 0xxxxxxx?
0080 - 07FF 110xxxxx 10xxxxxx?
0800 - FFFF 1110xxxx 10xxxxxx 10xxxxxx?
例如“漢”字的Unicode編碼是6C49。6C49在0800-FFFF之間,所以肯定要用3字節模板了:1110xxxx 10xxxxxx 10xxxxxx。將6C49寫成二進制是:0110 110001 001001, 用這個比特流依次代替模板中的x,得到:11100110 10110001 10001001,即E6 B1 89。
推薦使用UltraEdit進行編輯。
通過讀取漢字文本實現效果:
代碼:
begin//屏幕預處理cls()userPage()setTextMode(0)//關閉所有文件。 所有打開的文件都分配一個文件符,從0~9,最多10個。一個文件如果已經打開,再次打開則會出錯。這個文件符是全局的,如果打開后沒有關閉,即使應用程序關閉,在內存中關閉,文件也是處在打開狀態。因此建議每個文件打開是互斥事件,文件打開之前把所有的文件符都關閉。</span>文件指示符必須是全局變量,不能為局部變量$fileClose(0)$fileClose(1)$fileClose(2)$fileClose(3)$fileClose(4)$fileClose(5)$fileClose(6)$fileClose(7)$fileClose(8)$fileClose(9)//檢查文件是否存在if !$fileExists("Disk://Text.txt")popUpMsg("文件不存在!")returnendIf//打開文件nFileID=$fileOpen("Disk://Text.txt","r")if nFileID<0 or nFileID>9popUpMsg("文件打開錯誤!")returnendIf//讀取文件resize(sData,1,1)while $fileGet(nFileID,l_sBuff,1)==1if l_sBuff!=""sData[size(sData)-1]=l_sBuffappend(sData)endIfendWhile//去掉最后一行空白resize(sData,1,size(sData)-1)// 關閉文件$fileClose(0)$fileClose(1)$fileClose(2)$fileClose(3)$fileClose(4)$fileClose(5)$fileClose(6)$fileClose(7)$fileClose(8)$fileClose(9)//顯示文件title("詩歌鑒賞")//詩歌名for l_nIndex=0 to 1gotoxy((39-getDisplayLen(sData[l_nIndex]))/2,l_nIndex)put(sData[l_nIndex])endFor//輪流顯示while truefor l_nIndex=0 to 9gotoxy(0,l_nIndex+3)l_nNum=l_nNum+1if l_nNum==size(sData)l_nNum=2endIfl_nNum=max(2,l_nNum)put(left(sData[l_nNum]+" ",35))endFor delay(0.4)endWhileend<span style="font-size:12px;">
</span>