delphi ByteType-单双字节判断  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi ByteType-单双字节判断


//ByteType:判断是单字节还是双字节;
procedure TForm9.BitBtn3Click(Sender: TObject);
var s:System.AnsiString;
  iCount:Integer;
  c:AnsiString;
begin
s:='China Bank(中国银行)';
//ByteType:针对的是采用MBCS字符集的数据类型,对Unicode字符集无效,总是会返回mbSingleByte;
for iCount := 1 to System.Length(s) do
  begin
    //mbSingleByte:单字节;
    //mbLeadByte:双字节前半;
    //mbTrailByte:双字节后半;
    if SysUtils.ByteType(s,iCount)=mbSingleByte then
       Memo1.Lines.Add(s[iCount])
    else
      if SysUtils.ByteType(s,iCount)=mbLeadByte then
         begin
           c:=System.Copy(s,iCount,2);
           Memo2.Lines.Add(c);
         end;
  end;
end;
//附图:
ByteType-单双字节判断 - 朝三暮四 - 朝三暮四的博客
//统计汉字个数;
procedure TForm9.BitBtn3Click(Sender: TObject);
var s:System.AnsiString;
  iCount:Integer;
  c:AnsiString;
begin
s:='China Bank(中國银行)';
//s在内存的储存形式是:s[n]...s[1]s[0];计算机的CPU是从右向左方向进行读取;
for iCount := 1 to System.Length(s) do
  begin
    if (SysUtils.ByteType(s,iCount)=mbLeadByte) then
       //GBK汉字编码部分分三片区:
       //第一片区(GBK/2:GB2312部分):高字节范围从$A1..$FE,低字节范围从$B0..$F7;
       //第二片区(GBK/3:扩充汉字部分):高字节范围从$40..$FE,低字节范围从$81..$A0;
       //第三片区(GBK/4:扩充汉字部分):高字节范围从$40..$A0,低字节范围从$AA..$FE;
       if ((PByte(@s[iCount+1])^ in [$A1..$FE]) and (PByte(@s[iCount])^ in [$B0..$F7])) or
          ((PByte(@s[iCount+1])^ in [$40..$FE]) and (PByte(@s[iCount])^ in [$81..$A0])) or
          ((PByte(@s[iCount+1])^ in [$40..$A0]) and (PByte(@s[iCount])^ in [$AA..$FE])) then
          begin
            c:=System.Copy(s,iCount,2);
            Memo1.Lines.Add(c);
          end;
  end;
end;
//附图:
ByteType-单双字节判断
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.096090078353882 seconds