找了好多方法都不对正常二进制 1010110011110001000011011101001000001110010000111010
应该生成十六进制 ACF10DD20E43A下面转换方法却是DD2BB34Afunction BinToHex(Bin: String): String;
var
  i : integer;
  tmp : integer;
  iLen : integer;
begin
  tmp := 0;
  iLen := Length(Bin);
  for i := 1 to iLen do begin
  tmp := tmp +Min(StrToIntDef(Bin[i],0),1) shl (iLen - i);
  end;
  Result := Format('%.2X',[tmp]);
end;

解决方案 »

  1.   

    在classes   Unit
    procedure   BinToHex(Buffer,   Text:   PChar;   BufSize:   Integer);   assembler;
    const
        Convert:   array[0..15]   of   Char   =   '0123456789ABCDEF ';
    var
        I:   Integer;
    begin
        for   I   :=   0   to   BufSize   -   1   do
        begin
            Text[0]   :=   Convert[Byte(Buffer[I])  shr   4];
            Text[1]   :=   Convert[Byte(Buffer[I])  and   $F];
            Inc(Text,   2);
        end;
    end;
      

  2.   

    1010110011110001000011011101001000001110010000111010
    是个52位数!tmp 是个 integer;令外,你试试 showmessage(Format('%.2X',[1 shl 51])); 能否通过编译。
      

  3.   


    不正确啊
    procedure TForm1.Button1Click(Sender: TObject);
    var   buffer:pchar;
    str:string;
    leng:Integer;
    begin
    str:='1010110011110001000011011101001000001110010000111010';
    leng:=Length(str);
    getmem(buffer,leng);
    bintohex(PChar(str),buffer,leng);
    showmessage(buffer);
    freemem(buffer);
    end;
      

  4.   

    function BinstrToHex(Bin: String;var Rustr: String):boolean;
    var i, iLen : integer;
        tmp, a : Int64;
        q:array[1..63]of Int64;
    begin
      Result := false;
      iLen := Length(Bin);
      if iLen > 63 then exit;
      a:=1;
      for i:=0 to 62 do q[i+1]:=a shl i;
      tmp := 0;
      for i:=1 to iLen do
        if StrToIntDef(Bin[i],0)>0 then
          tmp := tmp + q[iLen-i+1];
      Rustr := Format('%.2X',[tmp]);
      Result := true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var s,ss:string;
    begin
      s:='1010110011110001000011011101001000001110010000111010';
      if BinstrToHex(s,ss) then
        showmessage(ss)
      else
        showmessage('字符串太长');
    end;
      

  5.   

    procedure BinToHex(Buffer, Text: PChar; BufSize: Integer); assembler;
    const
      Convert: array[0..15] of Char = '0123456789ABCDEF';
    var
      I: Integer;
    begin
      for I := 0 to BufSize - 1 do
      begin
        Text[0] := Convert[Byte(Buffer[I]) shr 4];//高4位变为低4位,即是除以 16
        Text[1] := Convert[Byte(Buffer[I]) and $F];//取低4位,即是模 16
        Inc(Text, 2);
      end;
    end;由此可见,它是按 Buffer 的每个位来处理的,但没见加权。我真想不出如何用它来处理楼主的问题。
      

  6.   

    我的函数还可以改写为这样:function BinstrToHex(Bin: String;var Rustr: String):boolean;
    var i, iLen, x : integer;
        tmp, a : Int64;
        q:array[0..63]of Int64;
    begin
      Result := false;
      iLen := Length(Bin);
      if iLen > 63 then exit;//受 Int64 类型范围所限
      a:=1;
      q[0]:=0;
      for i:=1 to 63 do q[i]:=a shl (i-1);
      tmp := 0;
      for i:=1 to iLen do begin
        try
          x:=StrToInt(Bin[i]);//使用了 StrToIntDef 就无法识别字符串是否合法
          if x>1 then exit;
          tmp := tmp + q[iLen-i+1] * x;
        except
          exit;
        end;
      end;
      Rustr := Format('%.2X',[tmp]);
      Result := true;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var s,ss:string;
    begin
      s:='1010110011110001000011011101001000001110010000111010';
      if BinstrToHex(s,ss) then
        showmessage(ss)
      else
        showmessage('位数太多,或不是一个有效的二进制字符串,无法转换。');
    end;
      

  7.   

    classes中的这个函数用PCHAR传递参数,说明它活在底层,也就应该没有异常保护的措施…还是自己写吧。。
      

  8.   

    function wwBinToHex(szBin : string):string;
    function myConvert(szBin:string):string;
    const
    sB: array [0..15] of string =
    ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');
    sH: array [0..15] of string =
    ('0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
      var
        i : integer;
    begin
        for i := 0 to 15 do
          begin
            if sB[i] = szBin then
            begin
              Result :=  sH[i];
            end;
          end;
    end;
    var
      nLength : Integer;
      nMod    : Integer;
      nPos    : Integer;
      szHex   : string;
      i  : integer;
    begin
      nLength := Length(szBin);
      nMod := nLength mod 4;
      for i := 0 to nMod - 1 do
      begin
        szBin := '0' + szBin;
      end;
      nPos := 1;
      while (nPos < nLength + nMod)  do
      begin
        szHex := szHex + myConvert(Copy(szBin,nPos,4));
        nPos := nPos + 4;
      end;
      result := szHex;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      caption := wwBinToHex('1010110011110001000011011101001000001110010000111010');
    end;
      

  9.   


    function wwBinToHex(szBin : string):string;
    function myConvert(szBin:string):string;
    const
    sB: array [0..15] of string =
    ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');
    sH: array [0..15] of string =
    ('0', '1', '2', '3', '4', '5', '6', '7',
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
      var
        i : integer;
    begin
        for i := 0 to 15 do
          begin
            if sB[i] = szBin then
            begin
              Result :=  sH[i];
            end;
          end;
    end;
    var
      nLength : Integer;
      nMod    : Integer;
      nPos    : Integer;
      szHex   : string;
      i  : integer;
    begin
      nLength := Length(szBin);
      // 上面代码 这里要改一下
      nMod := (4 - nLength mod 4) mod 4 - 1; // 这一句写错了
      for i := 0 to nMod  do
      begin
        szBin := '0' + szBin;
      end;
      nPos := 1;
      while (nPos < nLength + nMod)  do
      begin
        szHex := szHex + myConvert(Copy(szBin,nPos,4));
        nPos := nPos + 4;
      end;
      result := szHex;
    end;
      

  10.   

    我这个IE9似乎没法排版:function BinToHex(Bin: String): String;
    const
       Hexs: array[0..15] of Char = '0123456789ABCDEF';
    var
      i, j, Count, n, v: Integer;
    begin
      Count := Length(Bin) div 4;
      SetLength(Result, Count);
      for i := 1 to Count do
      begin
        v := 0;
        n := i * 4;
        for j := n - 3 to n do
          v := (v shl 1) + Ord(Bin[j]) - $30;
        Result[i] := Hexs[v];
      end;
    end;
      

  11.   

    是的,很强!简练,思路和语句结构十分清晰。通过学习,我发现,阿发伯哥哥的函数没考虑不完整字节数的情况,传入的参数不是按字节倍数的二进制字符串,该函数将不能返回正确的值。比如,传入‘100000000’与‘10000000’的返回值相同,当然,这样的参数也许不该传进去。所以,我将它改为这样就ok了:function BinToHex(Bin: String): String;
    const
      Hexs: array[0..15] of Char = '0123456789ABCDEF';
    var
      i, j, Count, n, v: Integer;
    begin
      i := Length(Bin) mod 4;
      if i > 0 then 
        Bin := StringOfChar('0', 4-i) + Bin;
      Count := Length(Bin) div 4;
      SetLength(Result, Count);
      for i := 1 to Count do begin
        v := 0;
        n := i * 4;
        for j := n - 3 to n do
          v := (v shl 1) + Ord(Bin[j]) - $30;
        Result[i] := Hexs[v];
      end;
    end;
      

  12.   

    楼主那串数据是BCD码啊,BCD转成十六进制的问题。