Delphi Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
델파이 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
FreePascal/Lazarus
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
델마당
볼랜드포럼 광고 모집

델파이 팁&트릭
Delphi Programming Tip&Tricks
[128] Binary <-> Decimal
박정모 [] 4623 읽음    2001-12-22 13:58

[ Binary -> Decimal ]
function Pow( i, k : Integer): Integer;
var
  j,
  liCount : Integer;
begin
  if k > 0 then
     j := 2
  else
     j := 1;

  for liCount := 1 to k - 1 do
     j := j * 2;

  Result := j;
end;

function BinToDec( Str : String ) : Integer;
var
  i,
  Len,
  Res : Integer;
  Error: Boolean;
begin
  Error := False;
  Len := Length( Str );
  Res := 0;

  for i := 1 to Len do
     if ( Str[ i ] = '0' ) OR
        ( Str[ i ] = '1' ) then
        Res := Res + Pow( 2, Len - i ) * StrToInt( Str[ i ] )
     else
     begin
        MessageDlg( 'It is not a binary number', mtInformation, [ mbOK ], 0 );
        Error := True;
        Break;
     end;

  if Error = True then
     Result := 0
  else
     Result := Res;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text<>'' then
    Label2.Caption:=IntToStr( BinToDec( Edit1.Text ) );
end;

[ Decimal -> Binary ]
function TForm1.DecToBinStr( N : Integer ) : string;
var
  S: string;
  i: Integer;
  Negative: Boolean;
begin
  if N<0 then Negative:=True;
  N:=Abs(N);
  for i:=1 to SizeOf(N)*8 do
  begin
    if N<0 then S:=S+'1'
    else S:=S+'0';
    N:=N shl 1;
  end;
  Delete(S,1,Pos('1',S)-1);
  if Negative then S:='-'+S;
  Result:=S;
end;


+ -

관련 글 리스트
128 Binary <-> Decimal 박정모 4623 2001/12/22
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.