type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DecToOctStr(n: integer) : string;
var
Digit : byte;
begin
while (n > 0) do
begin
// 10진수를 2진수로 바꾸었을때 8진수는 3자리씩 끊어 표현
Digit := n and 7;
Result := IntToStr(Digit) + Result;
n := n shr 3; // 8 로 나누는 의미
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('10진수 11 => 8진수 '+DecToOctStr(11));
ShowMessage('10진수 98 => 8진수 '+DecToOctStr(98));
ShowMessage('10진수 327 => 8진수 '+DecToOctStr(327));
end;
그 제 경우 거의 같은데 Sign 추가에요 ^^
function DecToOctStr(value : integer) : string;
var
i, c : integer;
begin
Result := '';
c := (sizeof(integer) * 8) div 3 - 1;
for i := 0 to c do
result := inttostr((value shr (i * 3)) and 7) + result;
if Value < 0 then
result := '-' + inttostr(strtoint(result));
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DecToOctStr(n: integer) : string;
var
Digit : byte;
begin
while (n > 0) do
begin
// 10진수를 2진수로 바꾸었을때 8진수는 3자리씩 끊어 표현
Digit := n and 7;
Result := IntToStr(Digit) + Result;
n := n shr 3; // 8 로 나누는 의미
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('10진수 11 => 8진수 '+DecToOctStr(11));
ShowMessage('10진수 98 => 8진수 '+DecToOctStr(98));
ShowMessage('10진수 327 => 8진수 '+DecToOctStr(327));
end;
end.