Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the problem on this program? why it cant running ,the result should in edit2.text

What I have tried:

procedure TForm5.Button2Click(Sender: TObject);
function IsCharValidInBase( Value : Char; Base : integer) : boolean;
var
  iVal : integer;
begin
  case Value of
    '0'..'9': iVal := (Ord(Value)-Ord('0'));
    'A'..'Z': iVal := (Ord(Value)-Ord('A')+10);
    'a'..'z': iVal := (Ord(Value)-Ord('a')+10);
    else iVal := Base; // illegal value!
  end;
  Result := iVal < Base;
end;

function StrBaseToInt(Str: string; const Base : integer): Integer;
var
i: Integer;
begin
  if (Base > 36) or (Base < 2) then raise Exception.Create('Invalid Base');
  Result:=0;
  Str:=AnsiUpperCase(Str);
  for i:=1 to Length(Str) do
  begin
    if IsCharValidInBase( Str[i], Base) then
    begin
      case Str[i] of
        '1'..'9': Result:=Result * Base + (Ord(Str[i])-Ord('0'));
        'A'..'Z': Result:=Result * Base +(Ord(Str[i])-Ord('A')+10);
        'a'..'z': Result:=Result * Base +(Ord(Str[i])-Ord('a')+10);
        else raise Exception.Create( 'Illegal character found');
      end;
    end
    else
    begin
      raise Exception.Create( 'Illegal character found');
    end;
  end;
end;

function IntToStrBase( Value, Base : integer ) : string;
var
  iDigit : integer;
  iChar : string;
begin
  if Value = 0 then
  begin
    Result := '0';
  end
  else
  begin
    Result := '';
    while Value > 0 do
    begin
      iDigit := Value mod Base;
      Value := Value div Base;
      case  iDigit of
        0..9: iChar := Char( iDigit + ord('0'));
        10..36: iChar := Char( iDigit -10 + ord('A'));
      end;
      Result := iChar + Result;
    end;
  end;
end;

function HexToOct( Value : string ) : string;
Begin
Edit2.Text:= IntToStrBase( StrBaseToInt( Value, 8), 16 );
end;
end.
Posted
Comments
Patrice T 26-Nov-17 17:11pm    
Define "it cant running", got an error message? which one?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900