|
|
|
uses SysUtils; | |
function DecToBase(Decimal: LongInt;const Base: Byte): String; | |
function px(s: string): integer; | |
function ptype (int: integer): string; | |
function num(tp, nm: string): string; | |
function cut(str: string; fp, tp: integer): string; | |
function inv(str: string): string; | |
function bpln(str: string): string; | |
function DecToBin(str: string): string; | |
function HexToDec(str: string): string; | |
function BinToDec(str: string): string; | |
function DecToHex(str: string): string; | |
function dectobins (str: string): string; | |
function bintodecs(str: string): string; | |
function dectobins (str: string): string; | |
d := strtofloat ('0'+','+str); | |
if trunc(d) >= 1 then | |
res := (res+'1'); | |
res := (res+'0'); | |
function DecToBase(Decimal: LongInt;const Base: Byte): String; | |
Symbols: String[16] = '0123456789ABCDEF'; | |
scratch: String; | |
remainder: Byte; | |
remainder := Decimal mod Base; | |
scratch := Symbols[remainder + 1] + scratch; | |
Decimal := Decimal div Base; | |
function px(s: string): integer; | |
i, res: integer; | |
for i := 1 to length(s) do | |
if (s[i] = ',') and (res=0) then | |
res := i else if (s[i] = ',') and (res<>0) then | |
function ptype (int: integer): string; | |
if (int >= -128) and (int <= 127) then | |
if (int >= -32768) and (int <= 32767) then | |
if (int >= -2147483648) and (int <= 2147483647) then | |
function num(tp, nm: string): string; | |
len, n, i: integer; | |
if (tp = 'byte') and (len < 8) then | |
n := 8 - len; | |
for i := 1 to n do | |
nm := '0'+nm; | |
if (tp = 'word') and (len < 16) then | |
n := 16 - len; | |
for i := 1 to n do | |
nm := '0'+nm; | |
if (tp = 'dblword') and (len < 32) then | |
n := 32 - len; | |
for i := 1 to n do | |
nm := '0'+nm; | |
function cut(str: string; fp, tp: integer): string; | |
cut := copy (str, fp, length(str)-fp-(length(str)-tp)+1); | |
function inv(str: string): string; | |
for i := 1 to length (str) do | |
case str[i] of '1': str[i] := '0'; | |
'0': str[i] := '1'; | |
function bpln(str: string): string; | |
if str[c] = '0' then | |
ts[c] := '1'; | |
if str[c] = '1' then | |
ts[c] := '0'; | |
until (c = 0) or (fl = true); | |
if fl = true then bpln := ts; | |
if (c = 0) and (fl = false) then bpln := ('1'+ts); | |
function dectobin(str: string): string; | |
res, tmp, bin, s1, s2, | |
b1, b2, t1, ft1, ft2: string; | |
i := px (str); | |
if str[1]='-' then | |
begin | |
delete (str, 1, 1); | |
if i = 0 then | |
begin | |
bin := dectobase(strtoint(str), 2); | |
tmp := num (ptype(strtoint(str)), bin); | |
bin := inv(tmp); | |
res := bpln(bin); | |
end; | |
if i > 0 then | |
begin | |
s1 := cut (str, 1, i-2); | |
s2 := cut (str, i+1, length(str)); | |
ft1 := ptype(strtoint(s1)); | |
b1 := dectobase(strtoint(s1), 2); | |
b2 := dectobins(s2); | |
t1 := num (ft1, b1); | |
s1 := inv (t1); | |
res := (bpln(s1)+','+b2); | |
end; | |
end else | |
begin | |
if i = 0 then | |
res := dectobase(strtoint(str), 2); | |
if i > 0 then | |
begin | |
s1 := cut (str, 1, i-1); | |
s2 := cut (str, i+1, length(str)); | |
b1 := dectobase(strtoint(s1), 2); | |
b2 := dectobins(s2); | |
res := (b1+','+b2); | |
end; | |
end; | |
dectobin := res; | |
function HexToDec(str: string): string; | |
const hex : array['A'..'F'] of integer = (10,11,12,13,14,15); | |
int, i: integer; | |