?? zkutils.pas
字號:
unit zkUtils;
interface
uses
SysUtils, Windows, Classes, MD5,Variants;
type
TBit = 0..31;
const
{ 一些有用的ASCII字符 }
NULL = #0;
BACKSPACE = #8;
TAB = #9;
LF = #10;
CR = #13;
ESC = #27;
BLANK = #32;
CRLF = #13#10;
SPACE = BLANK;
function FormatPath(const Value:String;const DIVChar: Char = '\'): String;
{格式化路徑字符串使之以'\'結束}
{例子:FormatPath('E:\Temp') = 'E:\Temp\'}
function Replace(Str,s1,s2: String; CaseSensitive: Boolean): String;
{在目標字符串中替換所有子字符串}
{例子: Replace('We know what we want','we','I',false) = 'I Know what I want'}
function CopyFromChar(const s: String;const c: Char;const l: Integer): String;
{從目標字符串s的c所在位置開始復制l個字符}
{例子: CopyFromChar('Borland Delphi','a',3) = 'and'}
function Get_ComputerName: String;
{獲取當前計算機名稱}
{例子: Get_ComputerName = 'JSZXServer'}
function Get_IPAddress: String;
{獲取本地IP地址}
{例子: Get_IPAddress = '146.128.20.203'}
function Get_UserName: String;
{獲取當前計算機登錄用戶名稱}
{例子: Get_UserName = 'Gaisy'}
function GetSystemDir: String;
{獲取Windows系統目錄路徑包括驅動器}
{例子: GetSystemDir = 'C:\WINDOWS\SYSTEM\'}
function GetWinDir: String;
{獲取Windows目錄路徑包括驅動器}
{例子: GetWinDir = 'C:\WINDOWS\'}
function GetCurrentDir: String;
{獲取系統當前目錄路徑包括驅動器}
{例子: GetCurrentDir = 'C:\WINDOWS\'}
function GetInstallDir: String;
{返回exe文件的所在目錄路徑包括驅動器}
{例子: GetInstallDir = 'C:\PROGRAM FILES\BORLAND\DELPHI\BIN\'}
function GetFileDate(Filename: String): TDateTime;
{返回文件的日期}
function GetFileVersion(Filename: String): String;
{返回某一文件的版本信息}
function SQLString(const AStr: String): String;
{返回字符串的TSQL編碼}
function MD5Hash(const Source: String): String;
function DataFieldToString(VData: Variant): String;
function DataFieldToInt(VData: Variant): Integer;
function DataFieldToCurr(VData: Variant): Currency;
{-------------------------------------------------------------------------------
過程名: 位操作函數組
作者: gaisy
日期: 2004.06.14
參數: const Value: Integer; const TheBit: TBit
Value為操作數, TheBit為操作位
例子: if BitSet(27, 0) then ...;
判斷27第0位
-------------------------------------------------------------------------------}
function bitSet(const Value: Integer;const TheBit: TBit): Boolean;
function bitOn(const Value: Integer;const TheBit: TBit): Integer;
function bitOff(const Value: Integer;const TheBit: TBit): Integer;
function bitToggle(const Value: Integer;const TheBit: TBit): Integer;
function BigbitSet(const Value: LongInt;const TheBit: TBit): Boolean;
function BigbitOn(const Value: LongInt;const TheBit: TBit): LongInt;
function BigbitOff(const Value: LongInt;const TheBit: TBit): LongInt;
function BigbitToggle(const Value: LongInt;const TheBit: TBit): LongInt;
{-------------------------------------------------------------------------------
過程名: 去除字符串左右空格函數(擴展)
作者: gaisy
日期: 2004.06.14
參數: const S: String; C: Char
對字符串S去除左右為C的字符
返回值: String
功能描述:
-------------------------------------------------------------------------------}
function strTrimChL(const S: String; C: Char): String;
function strTrimChR(const S: String; C: Char): String;
function strTrim(const S: String): String;
/// <summary>
/// 分解字符串函數
/// </summary>
/// <param name="Source">源字符串</param>
/// <param name="ch">分割符</param>
/// <returns>包含分割結果的字符串表</returns>
function SplitString(const Source,ch:String): TStringList;
{-------------------------------------------------------------------------------
過程名: 關于日期編解碼的函數和過程
作者: gaisy
日期: 2004.06.14
參數: const Year: Integer; Month, Day: Word
返回值: TDateTime
功能描述:
-------------------------------------------------------------------------------}
function EncodeDate(const Year: Integer; Month, Day: Word): TDateTime;
procedure DecodeDate(Date: TDateTime;var Year, Month, Day: Word); overload;
procedure DecodeDate(Date: TDateTime;var Year: Integer;var Month, Day: Word); overload;
procedure DecodeDate(Date: TDateTime;var Year, Month, Day: Integer); overload;
{-------------------------------------------------------------------------------
函數名: DateStr
作者: gaisy
日期: 2004.06.14
參數: DateTime: TDateTime; DivChar: Char=' '
返回值: String
功能描述: 返回DataTime類型裝換成的字符串,以DivChar分隔
-------------------------------------------------------------------------------}
function DateStr(DateTime: TDateTime; DivChar: Char=' '): String;
{-------------------------------------------------------------------------------
過程名: Year、Month、Day
作者: gaisy
日期: 2004.06.14
參數: DateTime: TDateTime
返回值: Integer
功能描述: 返回日期的年月日
-------------------------------------------------------------------------------}
function Year(DateTime: TDateTime): Integer;
function Month(DateTime: TDateTime): Integer;
function Day(DateTime: TDateTime): Integer;
{-------------------------------------------------------------------------------
過程名: StrToHex、HexToStr
作者: gaisy
日期: 2004.06.14
參數: AData: String
返回值: String
功能描述: 轉換10進制數到16進制數
-------------------------------------------------------------------------------}
function StrToHex(AData: String): String;
function HexToStr(AData: String): String;
{-------------------------------------------------------------------------------
過程名: CreateFolder
作者: gaisy
日期: 2004.06.14
參數: const AFolderName: String
返回值: Boolean
功能描述: 建立文件夾
-------------------------------------------------------------------------------}
function CreateFolder(const AFolderName: String): Boolean;
{-------------------------------------------------------------------------------
過程名: IsInteger、IsNumeric
作者: gaisy
日期: 2004.06.14
參數: Value: String
返回值: Boolean
功能描述: 判斷一個數是否是整數、數字
-------------------------------------------------------------------------------}
function IsInteger(Value: String): Boolean;
function IsNumeric(Value: String): Boolean;
{-------------------------------------------------------------------------------
過程名: GetPYIndexChar
作者: gaisy
日期: 2004.06.14
參數: hzChar: String
返回值: Char
功能描述: 返回漢字的首母
-------------------------------------------------------------------------------}
Function GetPYIndexChar(const hzChar: String): Char;
{-------------------------------------------------------------------------------
過程名: NumberToChinese
作者: gaisy
日期: 2004.06.14
參數: n0 : Real
返回值: String
功能描述: 將數字轉換為中文表示,用作財務處理
-------------------------------------------------------------------------------}
Function NumberToChinese(const n0 : Real): String;
Function GetRandomString(const Len: Integer): String;
//主要用戶MD5運算結果的顯示
function ByteArrayToStr(D: array of Byte): String;
function CharArrayToStr(D: array of Char): String;
implementation
uses
Sockets;
const
DaysPerYear = 365.2422454; // Solar Year
DaysPerMonth = DaysPerYear / 12;
DateTimeBaseDay = -693593; // 1/1/0001
EncodeDateMaxYear = 9999;
SolarDifference = 1.7882454; // Difference of Juliab Calendar to Solar Calendar at 1/1/10000
DateTimeMaxDay = 2958466; // 12/31/EncodeDateMaxYear + 1;
function Get_ComputerName: String;
var
c: array[0..127] of Char;
sz: dword;
begin
sz := SizeOf(c);
GetComputerName(c, sz);
Result := c;
end;
function Get_UserName: String;
var
u: array[0..127] of Char;
sz: dword;
begin
sz := SizeOf(u);
GetUserName(u, sz);
Result := u;
end;
function Get_IPAddress: String;
var
tp: TTcpClient;
straddr: String;
begin
tp := TTcpClient.Create(nil);
tp.Close;
tp.Open;
straddr := tp.LocalHostAddr;
Result := straddr;
tp.Close;
tp.Free;
end;
function CopyFromChar(const s:String; const c:Char; const l:Integer):String;
var
i:Integer;
begin
i := Pos(c,s);
Result := Copy(s,i,l);
end;
function StrToHex(AData: String): String;
var
StrResult, Temp: String;
i: Integer;
begin
for i := 0 to Length(AData) - 1 do
begin
Temp := Format('%x', [Ord(AData[i + 1])]);
if Length(Temp) = 1 then Temp := '0' + Temp;
StrResult := StrResult + Temp;
end;
Result := StrResult;
end;
function HexToStr(AData: String): String;
function HexToInt(Hex: String): Integer;
var
Res, I: Integer;
Ch: Char;
begin
Res := 0;
for I := 0 to Length(Hex) - 1 do
begin
Ch := Hex[I + 1];
if (Ch >= '0') and (Ch <= '9') then
Res := Res * 16 + Ord(ch) - Ord('0')
else if (Ch >= 'A') and (Ch <= 'F') then
Res := Res * 16 + Ord(Ch) - Ord('A') + 10
else if (Ch >= 'a') and (Ch <= 'f') then
Res := Res * 16 + Ord(Ch) - Ord('a') + 10
end;
Result := Res;
end;
var
Str, Temp: String;
I: Integer;
begin
Str := '';
for I := 0 to Length(AData) div 2 - 1 do
begin
Temp := Copy(AData, I * 2 + 1, 2);
Str := Str + Chr(HexToInt(Temp));
end;
Result := Str;
end;
function strTrim(const S: String): String;
begin
Result:=StrTrimChR(StrTrimChL(S,BLANK),BLANK);
end;
function strTrimChL(const S: String; C: Char): String;
begin
Result:=S;
while (Length(Result)>0) and (Result[1]=C) do Delete(Result,1,1);
end;
function strTrimChR(const S: String; C: Char): String;
begin
Result:=S;
while (Length(Result)> 0) and (Result[Length(Result)]=C) do
Delete(Result,Length(Result),1);
end;
function bitSet(const Value: Integer; const TheBit: TBit): Boolean;
begin
Result := (Value and (1 shl TheBit)) <> 0;
end;
function bitOn(const Value: Integer; const TheBit: TBit): Integer;
begin
Result := Value or (1 shl TheBit);
end;
function bitOff(const Value: Integer; const TheBit: TBit): Integer;
begin
Result := Value and ((1 shl TheBit) xor $FFFFFFFF);
end;
function bitToggle(const Value: Integer; const TheBit: TBit): Integer;
begin
Result := Value xor (1 shl TheBit);
end;
function BigbitSet(const Value: LongInt; const TheBit: TBit): Boolean;
begin
Result := (Value and (1 shl TheBit)) <> 0;
end;
function BigbitOn(const Value: LongInt; const TheBit: TBit): LongInt;
begin
Result := Value or (1 shl TheBit);
end;
function BigbitOff(const Value: LongInt; const TheBit: TBit): LongInt;
begin
Result := Value and ((1 shl TheBit) xor $FFFFFFFFFFFFFFFF);
end;
function BigbitToggle(const Value: LongInt; const TheBit: TBit): LongInt;
begin
Result := Value xor (1 shl TheBit);
end;
function FormatPath(const Value:String; const DIVChar: Char = '\'): String;
begin
if (Value[Length(Value)]<>DIVChar) then
Result := Value + DIVChar
else
Result := Value;
end;
function Replace(Str,s1,s2:String; CaseSensitive: Boolean): String;
var
i: integer;
s,t: String;
begin
s := '';
t := Str;
repeat
if CaseSensitive then
i := pos(s1,t)
else
i := pos(LowerCase(s1),LowerCase(t));
if i>0 then
begin
s := s + Copy(t,1,i-1) + s2;
t := Copy(t,i+Length(s1),MaxInt);
end
else s := s+t;
until i<=0;
Result := s;
end;
function GetSystemDir: String;
var
p: PChar;
z: Integer;
begin
z := 255;
GetMem(p,z);
GetSystemDirectory(p,z);
Result := FormatPath(String(p));
FreeMem(p,z);
end;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -