yj850624 发表于 2014-6-3 21:35:19

关于编程中汉字的处理问题

本帖最后由 yj850624 于 2014-6-3 21:37 编辑

大家好:近来学习Delphi对汉字的处理,在网上找到了一段代码,引发了我许多的疑问。特来论坛求教!

问题1:下面这段代码(求汉字对应的首字母)中“word(x) shl 8+word(x)”是什么意思?尤其是“word(x)”和“word(x)”是什么意思?
function TForm1.py(x: string): char;
begin
case word(x) shl 8+word(x) of
    $B0A1..$B0C4:result:='A';
    $B0C5..$B2C0:result:='B';
    $B2C1..$B4ED:result:='C';
    $B4EE..$B6E9:result:='D';
    $B6EA..$B7A1:result:='E';
    $B7A2..$B8C0:result:='F';
    $B8C1..$B9FD:result:='G';
    $B9FE..$BBF6:result:='H';
    $BBF7..$BFA5:result:='J';
    $BFA6..$C0AB:result:='K';
    $C0AC..$C2E7:result:='L';
    $C2E8..$C4C2:result:='M';
    $C4C3..$C5B5:result:='N';
    $C5B6..$C5BD:result:='O';
    $C5BE..$C6D9:result:='P';
    $C6DA..$C8BA:result:='Q';
    $C8BB..$C8F5:result:='R';
    $C8F6..$CBF9:result:='S';
    $CBFA..$CDD9:result:='T';
    $CDDA..$CEF3:result:='W';
    $CEF4..$D188:result:='X';
    $D1B9..$D4D0:result:='Y';
    $D4D1..$D7F9:result:='Z';
   ELSE
    result:=char(32);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
hp:string;
begin
edit2.Text:='';
for i:=1 to (length(edit1.Text) div 2) do
begin
   hp:=copy(edit1.Text,i*2-1,2);
   edit2.Text:=edit2.Text+py(hp);
end;

end;



问题2:下面这段代码中(求汉字的区位码),“byte(str)”和“byte(str)”是什么意思?
procedure TForm1.Button1Click(Sender: TObject);
var
   str:string;
   hi,lo:integer;
begin
   str:=trim(edit2.Text);
   hi:=byte(str)-$a0;
   lo:=byte(str)-$a0;
   edit1.Text:=inttostr(hi)+inttostr(lo);
end;

end.


问题3:上面问题1、2中的word和byte都是数据类型,为什么在上面的代码中好像是个函数呢?并且在Delphi的帮助中查不到相关的用法信息。请大家告诉我这是什么用法?

问题4:汉字的编码有许多,我在编程时怎样告诉系统我用的汉字编码是GBK或Unicode?

阔怀 发表于 2015-8-5 14:49:12

好多

bowk 发表于 2015-8-5 20:14:43

問題1.
word(x) 意思是說把那個矩陣中x字串轉成Unicode碼(這要看delphi支援的是什麼碼,猜測為unicode碼)
後面的左移八次在+word(x) 是他判別的方式(這要自己去看編碼的規律)
問題2.
跟上面的是同樣的意思
問題3.
強制轉換數據類型
問題4.
這個問題我不知道

xintiandi 发表于 2016-1-10 19:49:48

围观让高人解答。
页: [1]
查看完整版本: 关于编程中汉字的处理问题