• delphi -- 进制转换 函数表


    1.16 TO 10

    ********************************************************

    16转10,否则输出-1

    function Hex(c: char): Integer;
    var
      x: Integer;
    begin
      if ( Ord(c)>= Ord('0')) and (Ord(c) <= Ord('9')) then
        x:= Ord(c) - Ord('0')
      else if (Ord(c) >= Ord('a')) and (Ord(c) <= Ord('f')) then
        x:= Ord(c) - Ord('a') + 10
      else if (Ord(c) >= Ord('A')) and (Ord(c) <= Ord('F')) then
        x:= Ord(c) - Ord('A') + 10
      else
        x:= -1;                                       //输入错误
      Result:= x;
    end;

     
    2.字符 TO 10

    ***************************************************************
    该函数接收1~2个,字符转换成功后输出对应10进制数的值,否则输出-1 

    function HexToInt(Str: string): Integer;
    var
      tmpInt1, tmpInt2: Integer;
    begin
      if Length(Str) = 1 then
      begin
        Result:= Hex(Str[1]);
      end
      else if Length(Str) = 2 then
      begin
        tmpInt1:= Hex(Str[1]);
        tmpInt2:= Hex(Str[2]);
        if (tmpInt1 = -1) or (tmpInt2 = -1) then
          Result:= -1
        else
          Result:= tmpInt1 * 16 + tmpInt2;
      end
      else
        Result:= -1;                                  //输入错误,转换失败
    end;

    3.字符串 TO ASCII码

    *******************************************************************
    字符串转换成ASCII码字符串 

    function StrToHexStr(const S: string): string;
    var
      i: Integer;
    begin
      for i:= 1 to Length(S) do
      begin
        if i = 1 then
          Result:= IntToHex(Ord(S[1]), 2)
        else
          Result:= Result + ' ' + IntToHex(Ord(S[i]), 2);
      end;
    end;

    4.字符串去掉所有空格

    ***********************************************************************

    该函数去掉字符串中所有空格

    function TrimAll(Str: string): string;
    var
      mLen, i: Integer;
    begin
      mLen:= Length(Str);
      //初始化返回值
      Result:= '';
      for i:= 0 to mLen do
      begin
        //是空格就去掉
        if Str[i] = '' then
          Continue;
        Result:= Result + Str[i];
      end;
    end;

    5.10 TO 2

    **************************************************
    十进制转换成二进制字符串 

    function DTob(decimal:longint):string;
    const symbols:string[16]='01';
    var
      scratch:string;
      remainder:byte;
    begin
      repeat
        remainder:=decimal mod 2;
        scratch:=symbols[remainder+1]+scratch;
        decimal:=decimal div 2;
      until(decimal=0);
      result:=scratch;
    end;

    6.10 TO16字符

    **************************************************
    1.十进制转换成十六进制字符串

    function DToHex(decimal:longint):string;
    const symbols:string[16]='0123456789abcdef';
    var
      scratch:string;
      remainder:byte;
    begin
      repeat
        remainder:=decimal mod 16;
        scratch:=symbols[remainder+1]+scratch;
        decimal:=decimal div 16;
      until(decimal=0);
      result:=scratch;
    end;

    2.十进制转十六进制字符

    var HexArr: array[1..15]of string= ('1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');

    function Int2Hex(Value: Integer): string;
        var
          iTemp: Integer;
          i: Integer;
        begin
          Result :='';
          i := 0;
          while i<4 do
          begin
            case i of
              0: iTemp := Value shr 24 and $FF;
              1: iTemp := Value shr 16 and $FF;
              2: iTemp := Value shr 8 and $FF;
              3: iTemp := Value and $FF;
            end;
            if not Boolean(iTemp) then Result := Result + '00'
            else
            begin
              Result := Result + HexArr[iTemp div 16];
              Result := Result + HexArr[iTemp mod 16];
            end;
            Inc(i);
          end;
        end;

    7.字符串 TO 16进制

    *************************************************************************

    Eidt1.Text:=StrToInt('$'+'0000FFFF');   //65535

     

     


    2>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    unit Unit2;


    interface
    uses
    SysUtils,Math;
    type
    TConversion = class
    public
        //10 进制 to 2,8,16 进制
        function inttoBitStr(intstr: string): string;
        function IntToHexStr(intStr: string): String;//10 = 2
        function IntToOStr(intstr : string): string;//?????

        //2进制 to 10,8,16 进制
        function BittoInt(BitStr:   String):   LongInt;// 2 = 10
        function BitStrToHextStr(const   BitStr   :   String)   :   String;//2 = 16
        function BitStrToOStr(const   BitStr   :   String)   :   String;//2 = 8

        //16 > 10 2   8 进制
        function HextoIntStr(HexStr: String): string;
        function HexToBitStr(HexStr:   string): string;
        function HexToOStr(HexStr:   string): string;
       
        //八进制转换成二进制字符串
        function OtoBitStr(O : string):string;
        function OtoIntStr(O : string):string;
        function OtoHexStr(O : string):string;
    end;
    var
    TC :TConversion;


    implementation

    { TConversion }
    //2 进制 to 10 进制

    function TConversion.BittoInt(BitStr: String): LongInt;
    var
          i,Size:   Integer;
    begin
          Result:=0;
          Size:=Length(BitStr);
          for   i:=Size   downto   1   do
          begin
            //例如 1010
              if Copy(BitStr,i,1)='1'   then
                Result:=Result+(1   shl   (Size-i));
          end;
    //第二种方法
    //二进制转换为十进制 start
    {
    VAR
    str : String;
    Int : Integer;
    i : integer;

    Str := UpperCase(Edit1.Text);
    Int := 0;
    FOR i := 1 TO Length(str) DO
        Int := Int * 2 + ORD(str[i]) - 48;
    Edit2.Text:=IntToStr(int);
    }
    //二进制转换为十进制 end;
    //第三中方法
    {
    function hextoint(s: string): Double;
    begin
    while Length(s) <>0 do
    begin              //2^(长度-1)次方
        if s[1]='1' then Result:=Result+power(2,Length(s)-1);
        s:=Copy(s,2,Length(s));
    end
    end;
    }
    end;

    function TConversion.BitStrToHextStr(const BitStr: String): String;
    var
    vD : Byte;
    I : Integer;
    vHextStr : String;
    vP : PChar;
    vLen : Integer;
    begin
    vLen := Length(BitStr);
    if vLen mod 4 > 0 then
    begin
        SetLength(vHextStr,   vLen   div   4   +   1);
        vLen := vlen div 4 + 1;
    end
    else
    begin
        SetLength(vHextStr,   vLen   div   4);
        vLen := vlen div 4 ;
    end;

    //初始化
    vD := 0;
    vP := PChar(BitStr)+length(BitStr)-1;
    I := 0;   //开始计数

    while vP^ <> #0 do
    begin
        if vp^ = '1' then
        begin
          case i of
          0:      vD :=vd+1;
          1:      vD :=vd+2;
          2:      vD :=vd+4;
          3:      vD :=vd+8;
          end;
        end;

        Dec(vP);
        Inc(I);
        if I = 4 then
        begin
          case vD of
            0..9 : vHextStr[vLen] := Chr(vD + $30);
            10..15 : vHextStr[vLen] := Chr(vD - 10 + $41);
          end;
          Dec(vLen);
          I   :=   0;
          vD   :=   0;
        end;
    end;

    if I > 0then
    begin
        case   vD   of
          0..9 : vHextStr[vLen] := Chr(vD + $30);
          10..15 : vHextStr[vLen] := Chr(vD + $41);
        end;
    end;

    Result := vHextStr;
    end;

    function TConversion.BitStrToOStr(const BitStr: String): String;
    var
    vD : Byte;
    I : Integer;
    vHextStr : String;
    vP : PChar;
    vLen : Integer;
    begin
    vLen := Length(BitStr);
    if vLen mod 3 > 0 then
    begin
        SetLength(vHextStr,   vLen   div   3   +   1);
        vLen := vlen div 3 + 1;
    end
    else
    begin
        SetLength(vHextStr,   vLen   div   3);
        vLen := vlen div 3 ;
    end;

    //初始化
    vD := 0;
    vP := PChar(BitStr)+length(BitStr)-1;
    I := 0;   //开始计数

    while vP^ <> #0 do
    begin
        if vp^ = '1' then
        begin
          case i of
          0:      vD :=vd+1;
          1:      vD :=vd+2;
          2:      vD :=vd+4;
          end;
        end;

        Dec(vP);
        Inc(I);
        if I = 3 then
        begin
          case vD of
            0..9 : vHextStr[vLen] := Chr(vD + $30);
          end;
          Dec(vLen);
          I   :=   0;
          vD   :=   0;
        end;
    end;

    if I > 0then
    begin
        case   vD   of
          0..9 : vHextStr[vLen] := Chr(vD + $30);
        end;
    end;

    Result := vHextStr;
    end;

    function TConversion.HexToBitStr(HexStr: string): string;
    const
          cBitStrings:   array[0..15]   of   string   =
    (
    '0000',   '0001',   '0010',   '0011',
    '0100',   '0101',   '0110',   '0111',
    '1000',   '1001',   '1010',   '1011',
    '1100',   '1101',   '1110',   '1111'
    );
    var
    I:   Integer;
    begin
    Result   :=   '';
    for   I   :=   1   to   Length(HexStr)   do
          Result   :=   Result   +   cBitStrings[StrToIntDef('$'   +   HexStr[I],   0)];
    while   Pos('0',   Result)   =   1   do   Delete(Result,   1,   1);
    end;   {   HexToBit   }

    function TConversion.HextoIntStr(HexStr: String): string;
    begin
    result:= IntToStr(StrToInt('$' + (HexStr)));
    end;

    function TConversion.HexToOStr(HexStr: string): string;
    begin
    Result := BitStrToOStr(HexToBitStr(HexStr));
    end;

    function TConversion.inttoBitStr(intstr: string): string;
    var
    i :Integer;
    begin
    i := StrToInt(intstr);
    while i <>0 do
    begin              //i mod 2取模,再使用format格式化
       result:=Format('%d'+result,[i mod 2]);
       i:=i div 2
    end
    end;
    //10进制装换 2进制 第二种方法
    {function TConversion.IntToBitStr(Value, Size: Integer): String;
    var
    i: Integer;
    begin
    Result:='';
    for i:=Size-1 downto 0 do begin
        if Value and (1 shl i)<>0 then begin
           Result:=Result+'1';
    end else begin
           Result:=Result+'0';
    end;
    end;
    end;}


    function TConversion.IntToHexStr(intStr: string): String;
    begin
        Result:=inttoBitStr(intstr);
    end;

    function TConversion.IntToOStr(intstr: string): string;
    begin
    Result := BitStrToHextStr(inttoBitStr(intstr));
    end;

    function TConversion.OtoBitStr(O: string): string;
    const
          cBitStrings:   array[0..7]   of   string   =
    (
    '000',   '001',   '010',   '011',
    '100',   '101',   '110',   '111'
    );  
    var
    i,j:   Integer;
    begin
    Result   :=   '';
    for   I   :=   1   to   Length(o)   do
    begin
        j:=strtoint(o[i]);
        Result   :=   Result   +   cBitStrings[j];
    end;
    while   Pos('0',   Result)   =   1   do   Delete(Result,   1,   1);
    end;
    function TConversion.OtoHexStr(O: string): string;
    begin
    Result :=BitStrToHextStr(OtoBitStr(o));
    end;

    function TConversion.OtoIntStr(O: string): string;
    begin
    Result := OToIntStr(OtoBitStr(o));
    end;

    end.

  • 相关阅读:
    自动化测试如何解析excel文件?
    Unittest加载执行用例的方法总结
    pytest进阶之配置文件
    [编程题] 把二叉树打印成多行
    [编程题]求1+2+3+....n
    [编程题]-[位运算技巧系列]不用加减乘除做加法
    [编程题]数值的整数次方
    [编程题]构建乘积数组
    [编程题]变态跳台阶
    [编程题][剑指 Offer 10- II. 青蛙跳台阶问题]
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/4643181.html
Copyright © 2020-2023  润新知