delphi中常用字符处理函数(系统未提供的)
来源:互联网 发布:c语言数据结构和算法 编辑:程序博客网 时间:2024/05/19 23:05
unit toolfun;
interface
uses classes,forms,sysutils,variants,windows;
//string and data type convertion
function L_ByteToHex(const SrcByte:BYTE):string;stdcall;
function L_ByteToBin(const SrcByte:BYTE):string;stdcall;
function L_HexToByte(const SrcStr:string):integer;stdcall;
function L_BinToByte(const SrcStr:string):integer;stdcall;
//check
function StrValCheck(const SrcStr:string;const minb:integer=0;const maxb:integer=255):Boolean;stdcall;overload;
function StrValCheck(const SrcStr:string;allowchars:string):Boolean;stdcall;overload;
function StrisNum(const SrcStr:string):Boolean;stdcall;
//multi language
function UnicodeToBytes(const srcstr:widestring):string;
function BytesToUnicode(const srcstr:string):Widestring;
implementation
function L_ByteToHex(const SrcByte:BYTE):string;stdcall;
begin
result:=inttohex(SrcByte,2)
end;
function L_ByteToBin(const SrcByte:BYTE):string;stdcall;
var tbt:BYTE;
tret:string;
begin
tbt:=SrcByte;tret:='http://redstar.8j.cn/';
while tbt>0 do
begin
tret:=inttostr(tbt mod 2)+tret;
tbt:=tbt div 2;
end;
while length(tret)<8 do
tret:='0'+tret;
result:=tret;
end;
function L_HexToByte(const SrcStr:string):integer;stdcall;
var tstr:string;
begin
tstr:=sysutils.UpperCase(SrcStr);
if StrValCheck(tstr,ord('0'),ord('F')) then
begin
while length(tstr)<2 do
tstr:='http://redstar.8j.cn/0'+tstr;
result:=strtoint('$'+tstr);
end
else
Result:=-1;
end;
function L_BinToByte(const SrcStr:string):integer;stdcall;
var tstr:string;
i,tret,tpo:integer;
begin
tstr:=SrcStr;
if StrValCheck(tstr,ord('0'),ord('1')) then
begin
tret:=0;tpo:=1;
while length(tstr)<8 do
tstr:='http://redstar.8j.cn/0'+tstr;
for i:=1 to 8 do
begin
tret:=tret+tpo*strtoint(tstr[9-i]);
tpo:=tpo*2;
end;
result:=tret;
end
else
Result:=-1;
end;
function StrValCheck(const SrcStr:string;const minb:integer=0;const maxb:integer=255):Boolean;
var i:integer;
begin
Result:=false;
for i:=1 to length(SrcStr) do
if (ord(SrcStr[i])<minb) or (ord(SrcStr[i])>maxb) then
exit;
Result:=true;
end;
//使用指针操作 //正常字符串操作,效率差不多
function StrValCheck(const SrcStr:string;allowchars:string):Boolean; //function StrValCheck(const SrcStr:string;allowchars:string):Boolean;
var p:pchar; //var l:cardinal;
l:cardinal; //begin
begin // result:=false;
result:=false; // l:=length(srcstr);
p:=@SrcStr[1]; // while l>0 do
l:=length(srcstr); // begin
if l<=0 then exit; // if pos(srcstr[l],allowchars)<=0 then
while l>0 do // begin
begin // result:=false;
if pos(p[l-1],allowchars)<=0 then // exit;
begin // end;
result:=false; // inc(l,-1);
exit; // end;
end; // Result:=true;
inc(l,-1); //end;
end; //
Result:=true; //代码简单
end; //
function StrisNum(const SrcStr:string):Boolean;
begin
result:=StrValCheck(SrcStr,ord('0'),ord('9'));
end;
function UnicodeToBytes(const srcstr:widestring):string;
var tmpp:pchar;
i,tl:integer;
tmpret:string;
begin
tl:=length(srcstr);
getmem(tmpp,tl*2);
copymemory(tmpp,pointer(srcstr),tl*2);
for i:=0 to tl-1 do
begin
tmpret:=tmpret+tmpp[2*i+1]+tmpp[2*i];
end;
result:=tmpret;
freemem(tmpp);
end;
function BytesToUnicode(const srcstr:string):Widestring;
var tmpp:pchar;
i,tl:integer;
tmpret:Widestring;
begin
tl:=length(srcstr);
tmpret:='http://redstar.8j.cn/';
getmem(tmpp,tl);
copymemory(tmpp,pointer(srcstr),tl);
for i:=0 to (tl div 2)-1 do
tmpret:=tmpret+widechar(ord(tmpp[2*i])*256+ord(tmpp[2*i+1]));
result:=tmpret;
freemem(tmpp);
end;
end.
- delphi中常用字符处理函数(系统未提供的)
- Delphi中常用字符串处理函数
- Delphi中常用字符串处理函数
- Delphi中常用字符串处理函数
- Delphi中常用字符串处理函数
- 常用的字符处理函数
- C中常用的字符、字符串处理函数
- Delphi的字符处理
- Delphi常用获取系统信息的函数
- Delphi中常用的函数和属性
- Delphi常用系统函数总结
- Delphi常用系统函数总结
- Delphi常用系统函数总结
- Delphi常用系统函数总结
- Delphi常用系统函数总结
- Delphi的常用函数
- VC中常用的字符类型及相应的字符串处理函数
- [delphi技术]常用的几个字符串处理函数
- 防止盗链下载问题
- 我的写作规划
- Linux 的病毒发展史及分类
- 关于SSL配置的报告
- delphi中为自定控件绘制windows按钮
- delphi中常用字符处理函数(系统未提供的)
- ASP.NET虚拟主机的重大安全隐患
- 病毒的本质
- the instructuion of ARMLinux kernel Compile
- Windows XP下Eclipse+CDT+MinGW+wxWidgets软件开发
- .NET中的密码学--对称加密
- 捉摸不定的C语言
- 20种看asp源码的方法及工具
- Google真的那么容易寄钱给你吗?