怎么把二进制文件已16进制的方式读取到一个String中?

解决方案 »

  1.   

    我这个是二进制转十六进制的源码:
    const   
          cBinStrings:   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,   J,   L:   Integer;   
          S:   string;   
      begin   
          S   :=   '';   
          L   :=   Length(mBin);   
          if   L   mod   4   <>   0   then   
              for   I   :=   1   to   4   -   (L   mod   4)   do   
                  mBin   :=   '0'   +   mBin;   
        
          for   I   :=   Length(mBin)   downto   1   do   begin   
              S   :=   mBin[I]   +   S;   
              if   Length(S)   =   4   then   begin   
                  for   J   :=   0   to   15   do   
                      if   S   =   cBinStrings[J]   then   begin   
                          S   :=   IntToHex(J,   1);   
                          Break;   
                      end;   
                  if   Length(S)   >   1   then   
                      Result   :=   '0'   +   Result   
                  else   Result   :=   S   +   Result;   
                  S   :=   '';   
              end   ;   
          end;   
      end;   {   BinToHex   }   
        
      function   HexToBin(mHex:   string):   string;   
      var   
          I:   Integer;   
      begin   
          Result   :=   '';   
          for   I   :=   1   to   Length(mHex)   do   
              Result   :=   Result   +   cBinStrings[StrToIntDef('$'   +   mHex[I],   0)];   
          while   Pos('0',   Result)   =   1   do   Delete(Result,   1,   1);   
      end;   {   HexToBin   }   
        
      procedure   TForm1.Button1Click(Sender:   TObject);   
      begin   
          Edit1.Text   :=   HexToBin(Edit2.Text);   
      end;   
        
      procedure   TForm1.Button2Click(Sender:   TObject);   
      begin   
          Edit2.Text   :=   BinToHex(Edit1.Text);   
      end;   
      
      

  2.   

    先把文件读到MemoryStream里面,然后一个一个地转换每个字节
      

  3.   

    按字节,一个一个的转换成相对应的ASCII字符。
      

  4.   

    String? 不可能。可以考虑用PCHAR 或者 PBYTE 或者 array of byte。
    比较好的使用MemoryStream.LoadFromFile.
      

  5.   

    Byte[] byteArray = System.IO.File.ReadAllBytes("k:\\photo.jpg");
    StringBuilder HexStringBuffer = new StringBuilder();
    foreach (byte b in byteArray)
    {
      HexStringBuffer.Append(b.ToString("X2") + " ");
    }
    String hexString = HexStringBuffer.ToString();
    hexString = hexString.Trim();