如何将字符  11110000    转换为十六进制 FO  

解决方案 »

  1.   

    procedure BinToHex(Buffer, Text: PChar; BufSize: Integer);
      

  2.   

    const
      cHexBinStrings: array[0..15] of string = //十六进制和二进制对照表
    (
    '0000', '0001', '0010', '0011',
    '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011',
    '1100', '1101', '1110', '1111'
    );function BinToHex( //二进制转换成十六进制
      mBin: string //二进制字符
    ): string; //返回十六进制字符
    var
      I, L: Integer;
      S: string;
    begin
      Result := '';
      if mBin = '' then Exit;
      mBin := '000' + mBin;
      L := Length(mBin);
      while L >= 4 do
      begin
        S := Copy(mBin, L - 3, MaxInt);
        Delete(mBin, L - 3, MaxInt);
        for I := Low(cHexBinStrings) to High(cHexBinStrings) do
          if S = cHexBinStrings[I] then
          begin
            Result := IntToHex(I, 0) + Result;
            Break;
          end;
        L := Length(mBin);
      end;
    end; { BinToHex }function HexToBin( //十六进制转换成二进制
      mHex: string //十六进制字符串
    ): string; //返回二进制字符串
    var
      I: Integer;
    begin
      Result := '';
      for I := 1 to Length(mHex) do
        Result := Result + cHexBinStrings[StrToIntDef('$' + mHex[I], 0)];
    end; { HexToBin }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := BinToHex('11110000');
    end;
      

  3.   

    //参考如下代码
    //....
      private
        { Private declarations }
        procedure SpeedButtonClick(Sender: TObject);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}const
      cHexBinStrings: array[0..15] of string = //十六进制和二进制对照表
    (
    '0000', '0001', '0010', '0011',
    '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011',
    '1100', '1101', '1110', '1111'
    );function BinToHex( //二进制转换成十六进制
      mBin: string //二进制字符
    ): string; //返回十六进制字符
    var
      I, L: Integer;
      S: string;
    begin
      Result := '';
      if mBin = '' then Exit;
      mBin := '000' + mBin;
      L := Length(mBin);
      while L >= 4 do
      begin
        S := Copy(mBin, L - 3, MaxInt);
        Delete(mBin, L - 3, MaxInt);
        for I := Low(cHexBinStrings) to High(cHexBinStrings) do
          if S = cHexBinStrings[I] then
          begin
            Result := IntToHex(I, 0) + Result;
            Break;
          end;
        L := Length(mBin);
      end;
    end; { BinToHex }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := BinToHex('11110000');
    end;var
      FSpeedButtons: array[0..7] of TSpeedButton;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: Integer;
    begin
      for I := 0 to 7 do
      begin
        FSpeedButtons[I] := TSpeedButton.Create(Self);
        with FSpeedButtons[I] do
        begin
          Parent := Self;
          Caption := IntToStr(8 - I);
          Left := Width * I;
          AllowAllUp := True;
          GroupIndex := I + 1;
          OnClick := SpeedButtonClick;
        end;
      end;
      Edit1.Text := '00000000';
    end;procedure TForm1.SpeedButtonClick(Sender: TObject);
    const
      cBoolChar: array[Boolean] of Char = '01';
    var
      I: Integer;
      S: string;
    begin
      S := '';
      for I := 0 to 7 do
        S := S + cBoolChar[FSpeedButtons[I].Down];
      Edit1.Text := S;
      Edit2.Text := BinToHex(S);
    end;
    //....