Delphi 里有没有取汉字ASC的函数,方法(100分)

F

ForCode

Unregistered / Unconfirmed
GUEST, unregistred user!
求救啊!
 
var
Str:String;
begin
Str:='张';
Caption:=IntToHex(PWord(@Str[1])^,4);
end;
 
这样也行
a:='我';
caption:=IntToStr(ord(a[1]))+IntToStr(ord(a[2]));
 
学习!
Caption:=IntToHex(PWord(@Str[1])^,4);
得到的是16进制的代码
caption:=IntToStr(ord(a[1]))+IntToStr(ord(a[2]));
得到的是10进制的代码!
不过好像应该是IntToStr(ord(a[2]))+IntToStr(ord(a[1]));
换一下位置!
 
汉字就是unicode,delphi里面有widestring类型,可以立即使用
var
s: widestring;
i: integer;
begin
s := 'I am 中国人';
for i := 1 to length(s)do
showmessage(inttostr(ord(s)));
end;

标准的写法,韩文,日文都可以这样处理。
 
再问一个低级的问题:
buf :array[1..4] of char;
buf[3],buf[4]存放的是汉字'中',如何将他取出来呢?谢了
 
var
buf:array[1..4] of char;
begin
Buf[3]:=Char($BC);
Buf[4]:=Char($BC);
Caption:=Copy(buf,3,2);
end;
 
给出另一种思路,类似于汇编
var
s:string[5];
p:^byte
i:integer;
begin
P:=@s;
p^:=2;
for i:=1 to 2do
begin
inc(p)
p^:=buf[i+2];
end;
caption:=s;
 
你前面说的用ord函数就行了, 后面说的其实你只要将这两个字节任意复制到你想要的地方就
行了,用数组也好,用指针也行, 你可以把双字节当英文的单字节来处理。
而要比较汉字, 你只要将这两个字节一一对比, 两个字节都相同,当然是一个汉字了。
一个汉字由两个字节组成,要判断连续的两个字节是否汉字,
关键是看其第一个字节是否满足 >=$a0 条件, 这个条件对GBK汉字都适用
而如果一个汉字是GB2312中定义的最常用的6763个汉字,
则第一个字节和第二个节字都必须满足>=$a0 条件的条件
showmessage(chr($CB)+chr($CE));
//显示 宋 字
宋字的区位码是: 4346
宋字的国标码是: CBCE (16进制)
另外来几个
showmessage(chr($B5)+chr($CB));
//显示 邓 字
showmessage(chr($D0)+chr($A1));
//显示 小 字
showmessage(chr($C6)+chr($BD));
//显示 平 字
下面有点代码, 你可以参考一下:
procedure GeneralSearch();
var
sTemp, sTemp1, sTemp2 : string;
sList :string;
i , j, k, strLength :integer;
nToLen :integer;
begin
nToLen := length(Form1.combobox1.Text);
k :=1;
while k <nToLendo
begin
stemp:=copy(Form1.combobox1.Text,k,2);
if (ord(stemp[1])<$a0) then
begin
Form1.label1.Caption :=sTemp;
Form1.label2.Caption :='不是汉字,请输入汉字';
exit;
end;
for i :=1 to LibNodo
begin
if stemp = sChinese then
begin
Form1.label1.Caption :=sChinese;
strLength :=length(sCode);
sTemp1:='';
for j :=1 to strlengthdo
begin
sTemp2 :=copy(sCode,j,1);
if sTemp2<>'|' then
Stemp1 :=Stemp1+sTemp2
else
Stemp1 :=Stemp1+' ';
end;
// Form1.label2.Caption :=STemp1;
{ sCode;}
sList :=STemp+' '+Stemp1;
Form1.ListBox1.Items.Add(sList);
break;
end;
end;
Form1.ComboBox1.Items.Add(sTemp);
k :=k+2;
end;

end;

(*************************************)
procedure SearchQuwei();
var
sTemp, sTemp1, sTemp2 : string;
i , j ,nLen :integer;
begin
sTemp1 :='';
sTemp2 :='';
stemp:=copy(Form1.combobox1.Text,1,2);
Form1.Label1.Caption :=sTemp;
nLen :=length(sTemp);
if nLen <2 then
begin
Form1.label2.Caption :='不是汉字,请输入汉字';
if sTemp ='' then
Form1.label2.Caption :='请输入待查询的汉字';
exit;
end;

sTemp1 :=copy(sTemp,1,1);
sTemp2 :=copy(sTemp,2,1);
i :=ord(sTemp1[1]) - $a0;
j :=ord(Stemp2[1]) - $a0;
str(i, sTemp1);
str(j, sTemp2);
if length(sTemp1) <2 then
sTemp1 :='0'+sTemp1;
if length(sTemp2) <2 then
sTemp2 :='0'+sTemp2;
if (i>=0) and (j>=0) then
Form1.label2.Caption :=sTemp1+sTemp2
else
Form1.label2.Caption :='不是汉字,请输入汉字';
Form1.ComboBox1.Items.Add(sTemp);
end;

(*************************************)
procedure SearchAscii();
var
sTemp, sTemp1, sTemp2 : string;
i , j, nLen :integer;
begin
sTemp1 :='';
sTemp2 :='';
sTemp :=copy(Form1.combobox1.Text,1,2);
nLen :=length(sTemp);
Form1.Label1.Caption :=sTemp;
sTemp1 :=copy(sTemp,1,1);
i :=ord(sTemp1[1]);
sTemp1 :=Format('%x', );
if nLen =1 then
begin
if (i>=0) then
Form1.label2.Caption :=sTemp1+' Hex'
else
Form1.label2.Caption :='不是汉字';
end;

if nLen = 2 then
begin
sTemp2 :=copy(sTemp,2,1);
j :=ord(Stemp2[1]);
sTemp2 :=Format('%x', [j]);
if (i>=0) or (j>=0) then
Form1.label2.Caption :=sTemp1+sTemp2+' Hex'
else
Form1.label2.Caption :='不是汉字';
end;
Form1.ComboBox1.Items.Add(sTemp);
end;

procedure SearchQpNot();
var
sTemp, sTemp2, sOld, sQpStr : string;
sTemp1 : array [1..50] of string;
i , j,k, kk, strLength,nToLen :integer;
begin
nToLen := length(Form1.combobox1.Text);
kk :=1;
while kk <nToLendo
begin
stemp:=copy(Form1.combobox1.Text,kk,2);
if (ord(stemp[1])<$a0) then
begin
Form1.label1.Caption :=sTemp;
Form1.label2.Caption :='不是汉字,请输入汉字';
exit;
end;
for i :=1 to LibNodo
begin
if stemp = sChinese then
begin
Form1.label1.Caption :=sChinese;
strLength :=length(sCode);
k :=1;
Stemp1[k] :='';
for j :=1 to strlengthdo
begin
sTemp2 :=copy(sCode,j,1);
if sTemp2 = '|' then
begin
k :=k+1;
sTemp1[k] :='';
end
else
if (sTemp2 <> '1') and
(sTemp2 <> '2') and
(sTemp2 <> '3') and
(sTemp2 <> '4')
then
Stemp1[k] :=Stemp1[k]+sTemp2
end;
sOld :=sTemp1[1];
sQpStr :=sTemp1[1];
for j :=2 to kdo
begin
if sOld <> sTemp1[j] then
begin
sQpStr :=sQpStr +' '+sTemp1[j];
sOld := sTemp1[j];
end;
end;
// Form1.label2.Caption :=sQpStr;
{ sCode;}
Form1.listbox1.Items.add(sTemp+' '+sQpStr);
break;
end;
end;
Form1.ComboBox1.Items.Add(sTemp);
kk :=kk+2;
end;
end;

 
To Adnil:
你这样转换后,用什么函数还原呢?
chr(ord(s)恐怕不行。
 
问得好,答得妙:)
 
to tosleep:
uses widechar(ord(s))
simple example:
var
c1: widechar;
c2: widechar;
i: integer;
s: widestring;
begin
s := '汉';
c1 := s[1];
i := ord(c1);
c2 := widechar(i);
showmessage(c2);
end;

奇怪上面个别人的发言,windows和delphi都已经有了对unicode的支持,为什么不用呢?
 
我答此问题,是因为正在写一个加密函数的原因。
比如:对一个汉字的异或及偏移用unicode效果不理想,用widechar()固然可以,
当转换后不如hex来的好。
由于我水平有限,私下认为二者都有一定做用,
 
惭愧。
刚试了一下,用unicode实现加密,效果也还可以,看来好的方法还得自己去想想才行,
不能下断言。
 
顶部