Re: Как закодировать?
- From
- Mike Makhov (2:5020/909)
- To
- Alexander Chislov
- Date
- 2002-10-02T08:28:02Z
- Area
- RU.ALGORITHMS
Hello Alexander.
Втp Окт 01 2030 16:42, Alexander Chislov wrote to All:
AC> From: Alexander Chislov <arch-vile@rnd.runnet.ru>
AC>
AC> Хай!
AC> Есть ли в Паскале такая стандартная функция, которая переводит число
AC> из десятичной системы счисления в двоичную?
Нет.
AC> Если нет, то как можно это алгоритмически сделать - закодировать
AC> десятичное число двоичным, и наоборот: двоичное перевести в десятичную
AC> форму.
Вот pешение "влоб":
function BinaryStrToDecimal(BinStr: string): integer;
var
i: integer;
begin
Result := 0;
for i:=Length(BinStr) downto 1 do
Result := Result + StrToInt(BinStr[i]) * Integer( Trunc(
Power( 2, Length(BinStr) - i )));
end;
function DecimalToBinaryStr( ANum: integer; StrLength: Byte ): string;
var
i: integer;
str: string;
begin
str := IntToHex( ANum, SizeOf(Integer) * 2 );
Result := '';
for i:=1 to SizeOf(Integer) * 2 do
case str[i] of
'0': Result := Result + '0000';
'1': Result := Result + '0001';
'2': Result := Result + '0010';
'3': Result := Result + '0011';
'4': Result := Result + '0100';
'5': Result := Result + '0101';
'6': Result := Result + '0110';
'7': Result := Result + '0111';
'8': Result := Result + '1000';
'9': Result := Result + '1001';
'A': Result := Result + '1010';
'B': Result := Result + '1011';
'C': Result := Result + '1100';
'D': Result := Result + '1101';
'E': Result := Result + '1110';
'F': Result := Result + '1111';
end;
if StrLength > 0 then
Result := Copy( Result, Length(Result)-StrLength+1, StrLength );
end;
Mike
P.S. Можно и cоптимизиpовать, но, обычно эти функции нужны для отобpажения =>
и так сойдет ;-(
--- GoldED/W32 3.00.Beta2+
* Origin: Bllizard Station (2:5020/909)