zoukankan
html css js c++ java
Delphi中实现全角转半角
function SbctoDbc(s:
string
):
string
;
var
nlength,i:integer;
str,ctmp,c1,c2:
string
;
{
在windows中,中文和全角字符都占两个字节,
并且使用了ascii chart
2
(codes
128
-
255
)。
全角字符的第一个字节总是被置为163,
而第二个字节则是相同半角字符码加上128(不包括空格)。
如半角a为65,则全角a则是163(第一个字节)、
193
(第二个字节,
128
+
65
)。
而对于中文来讲,它的第一个字节被置为大于163,(
如
'
阿
'
为:
176
162
),我们可以在检测到中文时不进行转换。
}
begin
nlength:
=
length(s);
if
(nlength
=
0
) then
exit;
str:
=
''
;
setlength(ctmp,nlength
+
1
);
ctmp:
=
s;
i:
=
1
;
while
(i
<=
nlength)
do
begin
c1:
=
ctmp[i];
c2:
=
ctmp[i
+
1
];
if
(c1
=
#
163
) then
//
如果是全角字符
begin
str:
=
str
+
chr(ord(c2[
1
])
-
128
);
inc(i,
2
);
continue
;
end;
if
(c1
>
#
163
) then
//
如果是汉字
begin
str:
=
str
+
c1;
str:
=
str
+
c2;
inc(i,
2
);
continue
;
end;
if
(c1
=
#
161
) and (c2
=
#
161
) then
//
如果是全角空格
begin
str:
=
str
+
'
'
;
inc(i,
2
);
continue
;
end;
str:
=
str
+
c1;
inc(i);
end;
result:
=
str;
end;
查看全文
相关阅读:
PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
PAT甲题题解-1029. Median (25)-求两序列的中位数,题目更新了之后不水了
PAT甲题题解-1028. List Sorting (25)-水排序
BZOJ 1492 货币兑换Cash
Codeforces 276D Little Girl and Maximum XOR
Codeforces 526E Transmitting Levels
Codeforces 335B Palindrome
BZOJ 2527 Meteors
Codeforces 449D Jzzhu and Numbers
FJ省队集训DAY4 T3
原文地址:https://www.cnblogs.com/sonicit/p/772409.html
最新文章
【转载】内存溢出等问题的排查思路
【转载】线程池是怎样工作的
java 性能优化:35 个小细节,让你提升 java 代码的运行效率
搜索练习题1215:迷宫
搜索练习题八皇后
搜索练习题八皇后问题
搜索练习题LETTERS
蓝桥杯试题 基础练习 十进制转十六进制
蓝桥杯试题 基础练习 特殊回文数
蓝桥杯试题 基础练习 杨辉三角形
热门文章
Fiddler抓包工具
计算机天翼网盘
蓝桥杯试题 基础练习 字母图形
PAT甲题题解-1063. Set Similarity (25)-set的使用
PAT甲题题解-1014. Waiting in Line (30)-模拟,优先级队列
PAT甲题题解-1003. Emergency (25)-最短路径+路径数目
PAT甲题题解-1034. Head of a Gang (30)-并查集
PAT甲题题解-1033. To Fill or Not to Fill (25)-模拟
PAT甲题题解-1032. Sharing (25)-链表水题
PAT甲题题解-1031. Hello World for U (20)-字符串处理,水
Copyright © 2011-2022 走看看