菜鸟提问(30分)

B

bearyan

Unregistered / Unconfirmed
GUEST, unregistred user!
在Delphi中,Chr(i)仅能转换成单字节字符,但我想变为双字节呢(即i>255)?
用那个函数???
等待中,马上给分;
 
H

hs-kill

Unregistered / Unconfirmed
GUEST, unregistred user!
一般的字符只要加上65248就行了
widechar(65248+i)
但是对于某些特殊字符,比如句号。 顿号、单/双引号‘’“” 书名号《》
就需要自己判断了
下面贴一个老帖子:
function SignHalfToFull(SingleStr: string): string;
var
i:integer;
temp,sinstr:string;
single:char;
HalfMark,FullMark:boolean;
begin
temp:='';
i:=1;
sinstr:=SingleStr;
HalfMark:=true;
FullMark:=true;
while i<=length(sinstr)do
begin
single:=sinstr;
if ord(single) in [32..47,58..64,91..96,123..126] then
//48~57 为数字
//65-90 为A-Z
//97-122 为a-z 如果你想转换他们就把上面的范围改成 32..126
//第33~126号共94个字符,去除英文和数字,其他为标点符号和运算符号
begin
if ord(single)=46 then
temp:=temp+'。';
if ord(single)=60 then
temp:=temp+'《';
if ord(single)=62 then
temp:=temp+'》';
if ord(single)=39 then
begin
if HalfMark=true then
temp:=temp+'‘'
else
temp:=temp+'’';
HalfMark:=Not HalfMark;
end;
if ord(single)=34 then
begin
if FullMark=true then
temp:=temp+'“'
else
temp:=temp+'”';
FullMark:=Not FullMark;
end;
if ((ord(single)<>34) and (ord(single)<>39) and (ord(single)<>46) and (ord(single)<>60) and (ord(single)<>62)) then
temp:=temp+#163+chr(ord(single)+128);
inc(i);
end
else
begin
temp:=temp+copy(sinstr,i,1);
Inc(i);
end;
end;
result:=temp;
end;
 
H

hs-kill

Unregistered / Unconfirmed
GUEST, unregistred user!
稍微修改了下,加了字母和数字的转换控制
//简单的转换函数,复杂的话恐怕要加上脚本标记才行,
//因为有很多情况全角字符对应半角字符是多对一的关系
//例如:< 《 < 〈 ≮ ≤;或者:[ 【 [ 〔 〖 「 『
function ConvertToChinses(ASource:string;
CovNum:boolean=false;
CovUChar:boolean=false;
CovLChar:boolean=false): string;
function ToChinesChar(AChar: Char): WChar;
begin
{通用的常规字符到全角字符的转化函数}
Result := WideChar(65248 + Ord(AChar));
end;

var
TmpWChar:string;
do
ubleFH:Boolean;
m,n,l,x,i:cardinal;
TmpChar:Char;
begin
Result := '';
do
ubleFH := True;
l:=length(ASource);
setlength(result,l*2);
{假设全部转换为双字节,分配足够大空间}
m:=1;
n:=1;
while m<ldo
begin
TmpChar:=ASource[m];
if (ord(TmpChar)>127) or (ord(TmpChar)<33) or
((Not CovNum) and (TmpChar in ['0'..'9'])) or
((Not CovUChar) and (TmpChar in ['A'..'Z'])) or
((Not CovLChar) and (TmpChar in ['a'..'z'])) then
begin
Result[n]:=TmpChar;
inc(m);
inc(n);
continue;
end;

case TmpChar of
'.':
begin
if (ASource[m-1] = '.') or (ASource[m+1] = '.') then
begin
TmpWChar := '·';
{省略号}
end
else
TmpWChar := '。';
{句号}
end;
'/':
TmpWChar := '、';
{顿号}
'"':
begin
ifdo
ubleFH then
TmpWChar := '“' {左双引号}
else
TmpWChar := '”';
{右双引号}
do
ubleFH := notdo
ubleFH;
end;
'''':
begin
ifdo
ubleFH then
TmpWChar := '‘' {左单引号}
else
TmpWChar := '’';
{右单引号}
do
ubleFH := notdo
ubleFH;
end;
'<':
TmpWChar := '《';
{左书名号}
'>':
TmpWChar := '》';
{右书名号}
else
TmpWChar := ToChinesChar(TmpChar);
end;
x:=length(TmpWChar);
for i:=0 to x-1do
Result[n+i]:=TmpWChar[i+1];
Inc(m);
Inc(n,x);
end;
setlength(result,n-1);
end;

测试:
memo2.Lines.Text:=ConvertToChinses(memo1.Lines.Text,true,true,true);
原文:
procedure TForm1.FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow.Create(self);
combobox1.Items.Clear;
for i:=0 to 9do
begin
str:='';
setlength(str,31);
for j:=1 to 30do
str[j]:=inttostr(i)[1];
str[31]:='A';
//检测是否显示全最后一个字符
combobox1.Items.Add(str);
end;
combobox1.Items.Add('短字符');
combobox1.Style:= csOwnerDrawFixed;
end;

转换后:
procedure TForm1。FormCreate(Sender: TObject);
var
i,j:byte;
str:string;
begin
_hint:=THintWindow。Create(self);
combobox1。Items。Clear;
for i:=0 to 9 do
begin
str:=‘’;
setlength(str,31);
for j:=1 to 30 do
str[j]:=inttostr(i)[1];
str[31]:=‘A’; //检测是否显示全最后一个字符
combobox1。Items。Add(str);
end;
combobox1。Items。Add(‘短字符’);
combobox1。Style:= csOwnerDrawFixed;
end;
 
B

bearyan

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢您如此详细的回复,但或许我问题的表述有点不清晰,让你误解了,不好意思~!
我想问的是,假如:字符‘1’的代码为49,因此,我用Chr(49)就可以得到字符‘1’;
但如果我想得到中文字‘啊’呢,其代码为45217;假如我用Chr(45217)得到的结果和Chr(161)是一样的,就是说超出255的高字节位被截掉了;
那么我怎么才能得到这个中文字呢?
分值我还是会给您,但是希望您可以解答我的问题!!谢谢!
 
H

hs-kill

Unregistered / Unconfirmed
GUEST, unregistred user!
-_-....直接用caption:=widechar(45217);就可以了
不过你的“啊”字unicode编码错了...应该是21834或者$554A
你不会是用ansi编码里前一个字符的编码*100+后一个字符的编码的到的吧....-_-
应该这样得到他的编码:
var
ws:widestring;
begin
ws:='啊';
caption:=inttostr(ord(ws[1]));
end;
 

Similar threads

S
回复
0
查看
961
swish
S
S
回复
0
查看
925
SUNSTONE的Delphi笔记
S
S
回复
0
查看
752
SUNSTONE的Delphi笔记
S
D
回复
0
查看
723
DelphiTeacher的专栏
D
D
回复
0
查看
725
DelphiTeacher的专栏
D
顶部