Val编程-读取汉字
? ? ?Val編程中,對(duì)于漢字的讀取不是很友好,利用fileget直接讀取記事本產(chǎn)生的文件字符串會(huì)導(dǎo)致亂碼的產(chǎn)生。因?yàn)閂al只支持使用utf-8進(jìn)行編碼,因此讀取的文本需要進(jìn)行utf-8格式轉(zhuǎn)換。
? ? ?在GBK中,漢字占兩個(gè)字節(jié)。并且每個(gè)字節(jié)都大于128.可以通過直接讀取位來進(jìn)行轉(zhuǎn)換來讀取漢字。通過讀取的兩個(gè)數(shù)字來獲得漢字的ASCII碼。
? ? ?而在utf-8中具有自己的編碼方式。模擬器中讀取的是用GBK編碼,而顯示的是UTF-8.
?
? ? ? ? ? ? ? ? ? ? ? ??
utf-8格式說明:
? ? 這是為傳輸而設(shè)計(jì)的編碼,其系列還有UTF-7和UTF-16
? ? 其中UTF-16和Unicode編碼大致一樣, UTF-8就是以8位為單元對(duì)Unicode進(jìn)行編碼。從Unicode到UTF-8的編碼方式如下:
Unicode編碼(16進(jìn)制) UTF-8 字節(jié)流(二進(jìn)制)?
0000 - 007F 0xxxxxxx?
0080 - 07FF 110xxxxx 10xxxxxx?
0800 - FFFF 1110xxxx 10xxxxxx 10xxxxxx?
例如“漢”字的Unicode編碼是6C49。6C49在0800-FFFF之間,所以肯定要用3字節(jié)模板了:1110xxxx 10xxxxxx 10xxxxxx。將6C49寫成二進(jìn)制是:0110 110001 001001, 用這個(gè)比特流依次代替模板中的x,得到:11100110 10110001 10001001,即E6 B1 89。
推薦使用UltraEdit進(jìn)行編輯。
通過讀取漢字文本實(shí)現(xiàn)效果:
代碼:
begin//屏幕預(yù)處理cls()userPage()setTextMode(0)//關(guān)閉所有文件$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("文件打開錯(cuò)誤!")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)// 關(guān)閉文件$fileClose(0)$fileClose(1)$fileClose(2)$fileClose(3)$fileClose(4)$fileClose(5)$fileClose(6)$fileClose(7)$fileClose(8)$fileClose(9)//顯示文件title("詩(shī)歌鑒賞")//詩(shī)歌名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
轉(zhuǎn)載于:https://www.cnblogs.com/shumaojie/archive/2013/03/21/2973221.html
總結(jié)
以上是生活随笔為你收集整理的Val编程-读取汉字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle数据库升级与补丁
- 下一篇: VS 2010 开发 ActiveX 自