1,我怎么做能把16进制转换为2进制  如:16进制$0bc转2进制000010111101
  2,我想得到16进制,或2进制第几位是多少  如:我想得到16进制$0bc第一位 0
初学谢谢!

解决方案 »

  1.   

    把16进制转成10进制,然后用for循环,MOD 再 DIV  十进制转二进制的算法。
    第二个问题嘛,用COPY
      

  2.   

    1.二進制 <==> 十進制 // Binary to Integer  function BinToInt(value: string): Integer;  var i, ivalueSize: Integer;  begin    Result := 0;    ivalueSize := Length(value);    for i := ivalueSize downto 1 do      if value[i] = '1' then Result := Result + (1 shl (ivalueSize - i));  end;    // Integer to Binary  function IntToBin(value: Longint; Digits: Integer): string;  var i: Integer;  begin    Result := '';    for i := Digits downto 0 do      if value and (1 shl i) <> 0 then        Result := Result + '1'    else      Result := Result + '0';  end;    2.二進制 <==> 十六進制 // Hexadecimal to Binary function HexToBin(Hexadecimal: string): string;  const    BCD: 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    for i := Length(Hexadecimal) downto 1 do      Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result;  end;    // Binary to Hexadecimal function BinToHex(Binario:string):string; const       BCD: array [0..15] of string=         ('0000','0001','0010','0011','0100','0101','0110','0111',          '1000','1001','1010','1011','1100','1101','1110','1111');       HEX: array [0..15] of char=         ('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'); var     i,n:integer;     sTemp:string;     sNibble:string; begin     Result:='';     sTemp:=Binario+Copy('000',1,Length(Binario) mod 4);     for i:=0 to (Length(Binario) shr 2)-1 do     begin      sNibble:=Copy(sTemp,(i shl 2)+1,4);      n:=8;      while (sNibble <> BCD[n]) do        if sNibble < BCD[n] then Dec(n) else Inc(n);      Result:=Result+HEX[n];     end; end;
      

  3.   

    1、
    function HexToBin2(str:string):string;
    var
        i:integer;
        len:integer;
        strValue:string;
        iTemp:Integer;
    begin
        strVAlue:='';
        iTemp:=StrToInt(str);
        len:=length(IntToStr(StrToInt(str)));
        while iTemp>0 do
        begin
            if (iTemp and 1)=0 then
                strValue:='0'+strValue
            else
                strValue:='1'+strValue;
            iTemp:=iTemp shr 1;
        end;
        Result:=strValue;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
        Label1.Caption:=HexToBin2(Edit1.Text);
    end;2、用Copy,因为是字符串,
    Label1.Caption:=Copy('$0bc',2,1);////从字符串第2个位置,拷贝一个字符。