📄 numbase.pas
字号:
unit NumBase;
interface
uses
Classes, SysUtils;
function StrToIntAsDec(var V: Integer; S: string): Boolean; forward;
function StrToIntAsHex(var V: Integer; S: string): Boolean; forward;
function StrToIntAsOct(var V: Integer; S: string): Boolean; forward;
function StrToIntAsBin(var V: Integer; S: string): Boolean; forward;
function IntToStrAsHex(var S: string; V: Integer): Boolean; forward;
function IntToStrAsOct(var S: string; V: Integer): Boolean; forward;
function IntToStrAsBin(var S: string; V: Integer): Boolean; forward;
function IsDecStr(S: string): Boolean; forward;
function IsHexStr(S: string): Boolean; forward;
function IsOctStr(S: string): Boolean; forward;
function IsBinStr(S: string): Boolean; forward;
function IsDecChar(C: Char): Boolean; forward;
function IsHexChar(C: Char): Boolean; forward;
function IsOctChar(C: Char): Boolean; forward;
function IsBinChar(C: Char): Boolean; forward;
function IsAlphaChar(C: Char): Boolean; forward;
function IsDigitChar(C: Char): Boolean; forward;
implementation
function StrToIntAsDec(var V: Integer; S: string): Boolean;
begin
Result := IsDecStr(S);
if not Result then Exit;
V := StrToInt(S);
Result := True;
end;
function StrToIntAsHex(var V: Integer; S: string): Boolean;
var
I, J: Integer;
begin
Result := IsHexStr(S);
if not Result then Exit;
V := 0;
S := UpperCase(S);
for I := 1 to Length(S) do
begin
if IsDigitChar(S[I]) then J := Ord(S[I]) - Ord('0')
else J := Ord(S[I]) - Ord('A') + 10;
V := (V shl 4) + J;
end;
Result := True;
end;
function StrToIntAsOct(var V: Integer; S: string): Boolean;
var
I, J: Integer;
begin
Result := IsOctStr(S);
if not Result then Exit;
V := 0;
for I := 1 to Length(S) do
begin
J := Ord(S[I]) - Ord('0');
V := (V shl 3) + J;
end;
Result := True;
end;
function StrToIntAsBin(var V: Integer; S: string): Boolean;
var
I, J: Integer;
begin
Result := IsBinStr(S);
if not Result then Exit;
V := 0;
for I := 1 to Length(S) do
begin
J := Ord(S[I]) - Ord('0');
V := (V shl 1) + J;
end;
Result := True;
end;
function IntToStrAsHex(var S: string; V: Integer): Boolean;
var
I: Integer;
begin
S := '';
repeat
I := V and $F;
V := V shr 4;
S := IntToHex(I, 1) + S;
until V = 0;
Result := True;
end;
function IntToStrAsOct(var S: string; V: Integer): Boolean;
var
I: Integer;
begin
S := '';
repeat
I := V and $7;
V := V shr 3;
S := IntToStr(I) + S;
until V = 0;
Result := True;
end;
function IntToStrAsBin(var S: string; V: Integer): Boolean;
var
I: Integer;
begin
S := '';
repeat
I := V and $1;
V := V shr 1;
S := IntToStr(I) + S;
until V = 0;
S := StringOfChar('0', 8 - Length(S)) + S;
Result := True;
end;
function IsDecStr(S: string): Boolean;
var
E, R: Integer;
begin
Val(S, R, E);
Result := E = 0;
E := R; //avoid hints
end;
function IsHexStr(S: string): Boolean;
var
I: Integer;
begin
for I := 1 to Length(S) do
begin
if not IsHexChar(S[I]) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsOctStr(S: string): Boolean;
var
I: Integer;
begin
for I := 1 to Length(S) do
begin
if not IsOctChar(S[I]) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsBinStr(S: string): Boolean;
var
I: Integer;
begin
for I := 1 to Length(S) do
begin
if not IsBinChar(S[I]) then
begin
Result := False;
Exit;
end;
end;
Result := True;
end;
function IsDecChar(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '9');
end;
function IsHexChar(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '9') or
(C >= 'A') and (C <= 'F') or
(C >= 'a') and (C <= 'f');
end;
function IsOctChar(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '7');
end;
function IsBinChar(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '1');
end;
function IsAlphaChar(C: Char): Boolean;
begin
Result := (C >= 'A') and (C <= 'Z') or
(C >= 'a') and (C <= 'z');
end;
function IsDigitChar(C: Char): Boolean;
begin
Result := (C >= '0') and (C <= '9');
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -