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.

 
原创粉丝点击