Перевод чисел в системы счисления

  1. function BinToInt(const Value: string): Integer;
  2.  
  3. var
  4.   i, strLen: Integer;
  5. begin
  6.   Result := 0;
  7.   strLen := Length(Value);
  8.   for i := 1 to strLen do
  9.     if Value[i] = '1' then
  10.       Result := Result or (1 shl (strLen - i))
  11.     else
  12.       Result := Result and not (1 shl (strLen - i));
  13. end;
  14.  
  15.  
  16. function dec2hex(value: dword): string[8];
  17. const
  18.   hexdigit = '0123456789ABCDEF';
  19. begin
  20.   while value <> 0 do
  21.   begin
  22.     dec2hex := hexdigit[succ(value and $F)];
  23.     value := value shr 4;
  24.   end;
  25.   if dec2hex = '' then dec2hex := '0';
  26. end;
  27.  
  28. function HexToInt(s: string): Integer;
  29. var
  30.   s: string;
  31. begin
  32.   s := '$' + ThatHexString;
  33.   result := StrToInt(a);
  34. end;
  35.  
  36. const HEX: array['A'..'F'] of INTEGER = (10, 11, 12, 13, 14, 15);
  37.  
  38. function HextToInt2(str: string;): Integer;
  39. var
  40.     Int, i: integer;
  41. begin
  42.   Int := 0;
  43.   for i := 1 to Length(str) do
  44.     if str[i] < 'A' then
  45.       Int := Int * 16 + ORD(str[i]) - 48
  46.     else
  47.       Int := Int * 16 + HEX[str[i]];
  48.   Result := Int;
  49. end.
Так, парочка показательных примеров...

  • +5
  • views 1244
  • XakepPRO XakepPRO
  • comments 12

Реклама

Мы в соцсетях

tw tg yt gt