分享

用 TBytesStream 类实现的读文件为十六进制字符的函数

 aaie_ 2011-10-11

 

  1. 代码文件:  
  2. --------------------------------------------------------------------------------  
  3.    
  4. unit Unit1;  
  5.   
  6. interface  
  7.   
  8. uses  
  9.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  10.   Dialogs, StdCtrls;  
  11.   
  12. type  
  13.   TForm1 = class(TForm)  
  14.     Memo1: TMemo;  
  15.     Memo2: TMemo;  
  16.     Button1: TButton;  
  17.     procedure Button1Click(Sender: TObject);  
  18.     procedure FormCreate(Sender: TObject);  
  19.   end;  
  20.   
  21. var  
  22.   Form1: TForm1;  
  23.   
  24. implementation  
  25.   
  26. {$R *.dfm}  
  27.   
  28. {读文件为十六进制字符的函数}  
  29. function ReadFileToHex(FileName: string): string;  
  30. var  
  31.   bs: TBytesStream;  
  32.   i: Integer;  
  33. begin  
  34.   Result := '';  
  35.   if not FileExists(FileName) then Exit;  
  36.   
  37.   bs := TBytesStream.Create;  
  38.   bs.LoadFromFile(FileName);  
  39.   for i := 0 to bs.Size - 1 do  
  40.     Result := Result + Format('%.2x ', [bs.Bytes[i]]);  
  41.   bs.Free;  
  42. end;  
  43.   
  44. {测试}  
  45. procedure TForm1.Button1Click(Sender: TObject);  
  46. const  
  47.   FilePath = 'c:\temp\Text.txt';  
  48. begin  
  49.   Memo1.Lines.SaveToFile(FilePath);  
  50.   Memo2.Text := ReadFileToHex(FilePath);  
  51. end;  
  52.   
  53. procedure TForm1.FormCreate(Sender: TObject);  
  54. begin  
  55.   Button1.Caption := '保存并读出十六进制 (注意读出的 0D 0A 是换行符)';  
  56. end;  
  57.   
  58. end.  
  59. --------------------------------------------------------------------------------  
  60. 窗体文件:  
  61. --------------------------------------------------------------------------------  
  62.    
  63. object Form1: TForm1  
  64.   Left = 0  
  65.   Top = 0  
  66.   Caption = 'Form1'  
  67.   ClientHeight = 149  
  68.   ClientWidth = 339  
  69.   Color = clBtnFace  
  70.   Font.Charset = DEFAULT_CHARSET  
  71.   Font.Color = clWindowText  
  72.   Font.Height = -11  
  73.   Font.Name = 'Tahoma'  
  74.   Font.Style = []  
  75.   OldCreateOrder = False  
  76.   OnCreate = FormCreate  
  77.   PixelsPerInch = 96  
  78.   TextHeight = 13  
  79.   object Memo1: TMemo  
  80.     Left = 0  
  81.     Top = 0  
  82.     Width = 160  
  83.     Height = 124  
  84.     Align = alLeft  
  85.     Lines.Strings = (  
  86.       'Memo1')  
  87.     ScrollBars = ssVertical  
  88.     TabOrder = 0  
  89.   end  
  90.   object Memo2: TMemo  
  91.     Left = 179  
  92.     Top = 0  
  93.     Width = 160  
  94.     Height = 124  
  95.     Align = alRight  
  96.     Lines.Strings = (  
  97.       'Memo2')  
  98.     ScrollBars = ssVertical  
  99.     TabOrder = 1  
  100.   end  
  101.   object Button1: TButton  
  102.     Left = 0  
  103.     Top = 124  
  104.     Width = 339  
  105.     Height = 25  
  106.     Align = alBottom  
  107.     Caption = 'Button1'  
  108.     TabOrder = 2  
  109.     OnClick = Button1Click  
  110.   end  
  111. end  

读文件到十六进制的函数(Delphi 7 下可用)

  

  1. {函数}  
  2. function ReadFileToHex(FileName: string): string;  
  3. var  
  4.   b: Byte;  
  5. begin  
  6.   Result := '';  
  7.   if not FileExists(FileName) then Exit;  
  8.   with TMemoryStream.Create do begin  
  9.     LoadFromFile(FileName);  
  10.     Position := 0;  
  11.     while Position < Size do  
  12.     begin  
  13.       ReadBuffer(b, 1);  
  14.       Result := Result + Format('%.2x ', [b]);  
  15.     end;  
  16.     Trim(Result);  
  17.     Free;  
  18.   end;  
  19. end;  
  20.   
  21. {调用}  
  22. procedure TForm1.Button1Click(Sender: TObject);  
  23. var  
  24.   str: string;  
  25. begin  
  26.   str := ReadFileToHex('c:\temp\test.txt');  
  27.   ShowMessage(str);  
  28. end;  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多