100分求汉字转换成GBK码(源码) ( 积分: 0 )

  • 主题发起人 主题发起人 kukow
  • 开始时间 开始时间
K

kukow

Unregistered / Unconfirmed
GUEST, unregistred user!
求一个函数可以把输入的汉字转换成GBK码<br>如'董'--&gt;B6AD<br>&nbsp;&nbsp;'洋'--&gt;BAA3<br>&nbsp;&nbsp;'蒋'--&gt;BDAF<br><br>在线等待<br>邮箱交流:kukow@163.com<br>QQ交流:93689974
 
----&nbsp;1.进入Delphi创建一个新工程:Project1<br><br>----&nbsp;2.在Form1上创建以下控件并填写属性:<br><br>控件类型&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;属性名称&nbsp;&nbsp;属性值<br>Edit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Search<br>ListBox&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SourceList<br>Items&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;输入一些字符串,如姓名等,用于提供检索数据<br>ListBox&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResultList<br>----&nbsp;3.键入以下两个函数<br><br>//&nbsp;获取指定汉字的拼音索引字母,如:“汉”的索引字母是“H”<br>function&nbsp;GetPYIndexChar(&nbsp;hzchar:string):char;<br>begin<br>&nbsp;case&nbsp;WORD(hzchar[1])&nbsp;shl&nbsp;8&nbsp;+&nbsp;WORD(hzchar[2])&nbsp;of<br>&nbsp;&nbsp;&nbsp;$B0A1..$B0C4&nbsp;:&nbsp;result&nbsp;:=&nbsp;'A';<br>&nbsp;&nbsp;&nbsp;$B0C5..$B2C0&nbsp;:&nbsp;result&nbsp;:=&nbsp;'B';<br>&nbsp;&nbsp;&nbsp;$B2C1..$B4ED&nbsp;:&nbsp;result&nbsp;:=&nbsp;'C';<br>&nbsp;&nbsp;&nbsp;$B4EE..$B6E9&nbsp;:&nbsp;result&nbsp;:=&nbsp;'D';<br>&nbsp;&nbsp;&nbsp;$B6EA..$B7A1&nbsp;:&nbsp;result&nbsp;:=&nbsp;'E';<br>&nbsp;&nbsp;&nbsp;$B7A2..$B8C0&nbsp;:&nbsp;result&nbsp;:=&nbsp;'F';<br>&nbsp;&nbsp;&nbsp;$B8C1..$B9FD&nbsp;:&nbsp;result&nbsp;:=&nbsp;'G';<br>&nbsp;&nbsp;&nbsp;$B9FE..$BBF6&nbsp;:&nbsp;result&nbsp;:=&nbsp;'H';<br>&nbsp;&nbsp;&nbsp;$BBF7..$BFA5&nbsp;:&nbsp;result&nbsp;:=&nbsp;'J';<br>&nbsp;&nbsp;&nbsp;$BFA6..$C0AB&nbsp;:&nbsp;result&nbsp;:=&nbsp;'K';<br>&nbsp;&nbsp;&nbsp;$C0AC..$C2E7&nbsp;:&nbsp;result&nbsp;:=&nbsp;'L';<br>&nbsp;&nbsp;&nbsp;$C2E8..$C4C2&nbsp;:&nbsp;result&nbsp;:=&nbsp;'M';<br>&nbsp;&nbsp;&nbsp;$C4C3..$C5B5&nbsp;:&nbsp;result&nbsp;:=&nbsp;'N';<br>&nbsp;&nbsp;&nbsp;$C5B6..$C5BD&nbsp;:&nbsp;result&nbsp;:=&nbsp;'O';<br>&nbsp;&nbsp;&nbsp;$C5BE..$C6D9&nbsp;:&nbsp;result&nbsp;:=&nbsp;'P';<br>&nbsp;&nbsp;&nbsp;$C6DA..$C8BA&nbsp;:&nbsp;result&nbsp;:=&nbsp;'Q';<br>&nbsp;&nbsp;&nbsp;$C8BB..$C8F5&nbsp;:&nbsp;result&nbsp;:=&nbsp;'R';<br>&nbsp;&nbsp;&nbsp;$C8F6..$CBF9&nbsp;:&nbsp;result&nbsp;:=&nbsp;'S';<br>&nbsp;&nbsp;&nbsp;$CBFA..$CDD9&nbsp;:&nbsp;result&nbsp;:=&nbsp;'T';<br>&nbsp;&nbsp;&nbsp;$CDDA..$CEF3&nbsp;:&nbsp;result&nbsp;:=&nbsp;'W';<br>&nbsp;&nbsp;&nbsp;$CEF4..$D188&nbsp;:&nbsp;result&nbsp;:=&nbsp;'X';<br>&nbsp;&nbsp;&nbsp;$D1B9..$D4D0&nbsp;:&nbsp;result&nbsp;:=&nbsp;'Y';<br>&nbsp;&nbsp;&nbsp;$D4D1..$D7F9&nbsp;:&nbsp;result&nbsp;:=&nbsp;'Z';<br>&nbsp;else<br>&nbsp;&nbsp;&nbsp;result&nbsp;:=&nbsp;char(0);<br>&nbsp;end;<br>end;<br><br>//&nbsp;在指定的字符串列表SourceStrs中检索符合拼音索引字符串<br>PYIndexStr的所有字符串,并返回。<br>function&nbsp;SearchByPYIndexStr<br>(&nbsp;SourceStrs:TStrings;<br>PYIndexStr:string):string;<br>label&nbsp;NotFound;<br>var<br>&nbsp;i,&nbsp;j&nbsp;&nbsp;&nbsp;:integer;<br>&nbsp;hzchar&nbsp;:string;<br>begin<br>&nbsp;for&nbsp;i:=0&nbsp;to&nbsp;SourceStrs.Count-1&nbsp;do<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;j:=1&nbsp;to&nbsp;Length(PYIndexStr)&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hzchar:=SourceStrs[2*j-1]&nbsp;<br>+&nbsp;SourceStrs[2*j];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(PYIndexStr[j]&lt;&gt;'?')&nbsp;and<br>(UpperCase(PYIndexStr[j])&nbsp;&lt;&gt;<br>GetPYIndexChar(hzchar))&nbsp;then&nbsp;goto&nbsp;NotFound;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;result=''&nbsp;then&nbsp;result&nbsp;:=&nbsp;SourceStrs<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;result&nbsp;:=&nbsp;result&nbsp;+&nbsp;Char<br>(13)&nbsp;+&nbsp;SourceStrs;<br>NotFound:<br>&nbsp;&nbsp;&nbsp;end;<br>end;<br><br>4.增加编辑框Search的OnChange事件:<br>procedure&nbsp;TForm1.SearchChange(Sender:&nbsp;TObject);<br>var&nbsp;ResultStr:string;<br>begin<br>&nbsp;ResultStr:='';<br>&nbsp;ResultList.Items.Text&nbsp;:=&nbsp;SearchByPYIndexStr<br>(Sourcelist.Items,&nbsp;Search.Text);<br>end;
 
to:icc<br>我是要个函数把输入的汉字转成GBK码?我只要这个函数!
 
function&nbsp;Chinese2MacCodeB(AiChinese:&nbsp;String):&nbsp;String;<br>Var<br>&nbsp;&nbsp;ch,&nbsp;cl&nbsp;:&nbsp;Integer;<br>Begin<br>&nbsp;&nbsp;ch&nbsp;:=&nbsp;Ord(AiChinese[1]);<br>&nbsp;&nbsp;cl&nbsp;:=&nbsp;Ord(AiChinese[2]);<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;&nbsp;inttohex((ch&nbsp;shl&nbsp;8)&nbsp;+&nbsp;cl,4);<br>end;<br>to:kukow<br>你这个问题只有0分,题目的100分那里来?&nbsp;&nbsp;&nbsp;&nbsp;-_-!<br>并且'洋'的编码应是D1F3
 
问题解决~谢谢:Power255<br>我发现这里检索不到这样的内容呢?<br>把我的答案贴上来给大家共享~<br><br>Function&nbsp;ChinesetoGBK(AiChinese:&nbsp;String):&nbsp;String;<br>var<br>&nbsp;&nbsp;ch,&nbsp;cl,&nbsp;I&nbsp;:&nbsp;Integer;<br>&nbsp;&nbsp;S&nbsp;:&nbsp;string;<br>Begin<br>&nbsp;&nbsp;S:='';<br>&nbsp;&nbsp;For&nbsp;I:=1&nbsp;to&nbsp;(Length(AiChinese)&nbsp;div&nbsp;2)&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;ch&nbsp;:=&nbsp;Ord(AiChinese[2*I-1]);<br>&nbsp;&nbsp;&nbsp;&nbsp;cl&nbsp;:=&nbsp;Ord(AiChinese[2*I]);<br>&nbsp;&nbsp;&nbsp;&nbsp;S&nbsp;:=S&nbsp;+&nbsp;inttohex((ch&nbsp;shl&nbsp;8)&nbsp;+&nbsp;cl,4);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;S;<br>end;<br><br>不过还有不少问题有待大家完善,比如&quot;全角&quot;,&quot;半角&quot;的问题等;
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
935
import
I
I
回复
0
查看
724
import
I
后退
顶部