zoukankan      html  css  js  c++  java
  • Delphi的二進制與八進制、十進制、十六進制轉換

    uses Math;

    //  二進制 --> 十進制

    function BinToTen(s: string): Extended;
    var
      I, iLen: Integer;
    begin
      Result := 0;
      iLen := Length(s);
      for I := 0 to iLen - 1 do
        Result := Result + StrToInt(s[iLen - I]) * Power(2, I);
    end;

    // 十進制 --> 二進制

    function TenToBin(I: Integer): string;
    var
      iMod, iDiv: Integer;
    begin
      Result := '';
      while I > 0 do
      begin
        iDiv := I div 2;
        iMod := I mod 2;
        I := iDiv;
        Result := IntToStr(iMod) + Result;
      end;
    end;

    // 二進制,八進制,十六進制 ---> 十進制

    function XXXToTen(s: string; base: Byte): Extended;
    var
      I, iLen: Integer;
    const
      Convert: string = '0123456789ABCDEF';
    begin
      Result := 0;
      s := UpperCase(s);
      iLen := Length(s);
      if base = 16 then
      begin
        for I := 0 to iLen - 1 do
          Result := Result + (Pos(s[iLen - I], Convert) - 1) * Power(base, I);
      end
      else
      begin
        for I := 0 to iLen - 1 do
          Result := Result + StrToInt(s[iLen - I]) * Power(base, I);
      end;
    end;

    //十進制 -->二進制,八進制,十六進制

    function TenToXXX(I: Integer; base: Byte): string;
    var
      iMod, iDiv: Integer;
    const
      Convert: array[0..15] of Char = '0123456789ABCDEF';
    begin

      Result := '';
      while I > 0 do
      begin
        iDiv := I div base;
        iMod := I mod base;
        I := iDiv;
        if base = 16 then
          Result := Convert[iMod] + Result
        else
          Result := IntToStr(iMod) + Result;
      end;
    end;

  • 相关阅读:
    【转载】搜索题目推荐
    HDU 4629 Burning 几何 + 扫描线
    HDU 4630 No Pain No Game 树状数组+离线查询
    SPOJ 416 Divisibility by 15 细节题
    【转载】树状数组题目
    SPOJ 274 Johnny and the Watermelon Plantation(TLE)
    SPOJ 227 Ordering the Soldiers 线段树 / 树状数组
    HDU 4620 Fruit Ninja Extreme 搜索
    Java序列化与反序列化
    Java IO包装流如何关闭?
  • 原文地址:https://www.cnblogs.com/markwu/p/1699747.html
Copyright © 2011-2022 走看看