The IsDBCSLeadByte function determines whether a character is a lead byte ?
that is, the first byte of a character in a double-byte character set (DBCS).
procedure TForm1.Button1Click(Sender: TObject);
var
SrcStr,DestStr,mstr:String;
i,n,len:Integer;
begin
SrcStr:='123呵呵!'#179'xixi'#255;
n:=Length(SrcStr);
SetLength(DestStr,4*n);
i:=1;
len:=0;
while i<=n do
begin
if Byte(SrcStr)>=$A1 then //参见http://www.delphibbs.com/delphibbs/dispq.asp?lid=614615
begin
if i<n then
if Byte(SrcStr[i+1])>=$A1 then
begin
Inc(len);
DestStr[len]:=SrcStr;
Inc(len);
Inc(i);
DestStr[len]:=SrcStr;
Inc(i);
continue;
end;
mstr:=IntToHex(Byte(SrcStr),2);
Inc(len);
DestStr[len]:='(';
Inc(len);
DestStr[len]:=mstr[1];
Inc(len);
DestStr[len]:=mstr[2];
Inc(len);
DestStr[len]:=')';
end
else begin
Inc(len);
DestStr[len]:=SrcStr;
end;
Inc(i);
end;
Caption:=Copy(DestStr,1,len);
end;
s := //一串字符串
t := '';
while Length(s) > 0 do begin
case ByteType(s, 1) of
mbSingleByte:
begin
if Ord(s[1]) in [0..31, 128..255] then
AppendStr(t, '#' + IntToStr(Ord(s[1])))
else AppendStr(t, s[1]);
Delete(s, 1, 1);
end;
mbLeadByte:
begin
AppendStr(t, Copy(s, 1, 2));
Delete(s, 1, 2);
end;
mbTrailByte:
raise Exception.Create('');
end;
end;
ShowMessage(t);