Q&A

  • 10진수를 8진수로 변환...
안녕하세요? 맨날 구경만 하면서 배우다가 첨 질문올려보네요...^^

제가 질문하고 싶은것은 진수변환에 관한 내용입니다.
10진수를 16진수로 바꾼건됐는데요...(format의 %x로...)
8진수로는 어떻게 바꾸는지 모르겠네요. 원래 없는건지....

그냥 간단히 에디트로 입력받아서 바꿀려고 하거든요.
먼가 방법있으면 좀 알려주세요....부탁드립니다.

ps. 참, 전 왜 IntToHex가 안돼죠? 델파이2라서 그런가????
    
     var     i : integer;
     begin
        i := strtoint(edit1.text);
        i := inttohex(i);
        edit2.text := inttostr(i);
     end;

     잘못된부분있으면 지적해주세요....감사~
3  COMMENTS
  • Profile
    이준해 2002.05.03 21:58
    // 알파벳을 이용한 진수 표현.. Max_Base 는 0..9, A..Z 까지를 의미함
    const
      MIN_BASE = 2;
      MAX_BASE = 36;

    // Char -> Integer
    function ConvertCharToInt(const Value: Char): Integer;
    begin
      case Value of
        '0'..'9':
          Result := Ord(Value) - Ord('0');
        'A'..'Z':
          Result := Ord(Value) - Ord('A') + 10;
        else
          raise Exception.Create('ConvertCharToInt: Argument out of range.');
      end;
    end;

    // Integer -> Char
    function ConvertIntToChar(const Value: Integer): Char;

    begin
      case Value of
        0..9:
          Result := Chr(Value + Ord('0'));
        10..35:
          Result := Chr(Value + Ord('A') - 10);
        else
          raise Exception.Create('ConvertIntToChar: Argument out of range.');
      end;
    end;

    // 각 진법에 의한 수를 10 진수로..
    function AnyBaseToInt64(const Value: String;
                            const OldBase: Integer): Int64;

    var
      i: Integer;
      tmpResult: Int64;
    begin
      // 진수의 밑을 Check (2-36)
      Assert(OldBase >= MIN_BASE);
      Assert(OldBase <= MAX_BASE);
      tmpResult := 0;
      // 진법의 계산방식 적용 (FFFF = 15*16^3 + 15*16^2 + 15*16 + 15)
      for i:=1 to length(Value) do begin
        tmpResult := tmpResult * OldBase;
        Inc(tmpResult, ConvertCharToInt(Value[i]));
      end;
      result := tmpResult;
    end;

    // 양수 10 진수를 -> 각 진법에 의한 수로
    function IntToAnyBase(const Value: Int64;
                          const NewBase: Integer;
                          const MinDigitCount: Integer = 0): String;

    var
      tmpValue: Int64;
      tmpResult: String;
    begin
      Assert(NewBase >= MIN_BASE);
      Assert(NewBase <= MAX_BASE);
      Assert(Value >= 0);

      tmpResult := '';
      tmpValue  := Value;
      repeat
        tmpResult := ConvertIntToChar(tmpValue mod NewBase) + tmpResult;
        tmpValue  := tmpValue div NewBase;
      until (tmpValue = 0);

      if (length(tmpResult) < MinDigitCount) then
        tmpResult := StringOfChar('0', MinDigitCount - length(tmpResult)) + tmpResult;

      result := tmpResult;
    end;


    // 10 진수를 8진수로 바꾸려면..

    function IntToOct(aValue: Int64): String;
    begin
       result := IntToAnyBase(aValue, 8);
    end;


    // 님께서 사용하신 IntToHex 는 문자열을 return 합니다.
    // 변수 i 에 넣을 수 없죠.

    도움이 되길..
    이준해
  • Profile
    이종직 2002.05.04 00:49
    햐아....꽤 길군요....
    정말 감사합니다. 이렇게 친절히 답변해주시고....^^
    많은 도움 됐습니다.


    * 추가 *****************************************

    켁....근데 아직도 inttohex가 안먹어요....ㅜㅜ

    var i : integer;
         s : string;
    begin
      i := strtoint(edit1.text);
      s := inttohex(i);    <- 여기서 에러가...(Not enough actual parameters.)
      edit2.text := s;
    end;

    아직도 먼가 틀린건가요?
    한번더 답변을 부탁드려도 될까요...??? 헤헤헤...

  • Profile
    김기수 2002.05.05 02:40
    도움말에 보면 이렇게 나와 있군요.

    function IntToHex(Value: Integer; Digits: Integer): string; overload;
    function IntToHex(Value: Int64; Digits: Integer): string; overload;

    Description

    IntToHex converts a number into a string containing the number's hexadecimal (base 16) representation. Value is the number to convert. Digits indicates the minimum number of hexadecimal digits to return.

    델파이 내장함수(프로시져)는 도움말을 찾아보는게 가장 빠릅니다.
서버에 요청 중입니다. 잠시만 기다려 주십시오...