• 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;

  • 相关阅读:
    android原子
    android进程优先级
    Android 的cyclicBarrier
    android中运用CountDownLatch
    java网络编程长连接的问题
    https
    http 上传文件
    netty4 断线重连
    Linux下高并发socket最大连接数所受的各种限制
    Android Studio NDK及so文件开发
  • 原文地址:https://www.cnblogs.com/markwu/p/1699747.html
Copyright © 2020-2023  润新知