将文档转成HEX格式输出

此程序可以将文档转成HEX格式输出。DELPHI7,WINXP编译通过
program phexview;
{$APPTYPE CONSOLE}
uses
  SysUtils,classes;
const
  dispChar=[#33..#224];
  modSp='- ';//分割字符
  nonDispChar='.';//不显示字符
  HexChar:array[0..15] of char=('0','1','2','3','4','5','6','7',
                           '8','9','A','B','C','D','E','F');
function ByteToHex(const bt :byte):string;
var
  caTmp:array[0..2] of char;
begin
  caTmp[1]:=HexChar[integer(bt) and $0000000f];
  caTmp[0]:=hexChar[bt shr 4];
  caTmp[2]:=#0;
  result:=caTmp;
end;

var
  msSrc:TMemoryStream;
  tmpStr,ascstr:string;
  i,m:integer;
  //tmpStr:pchar;
begin
  { TODO -oUser -cConsole Main : Insert code here }
  if ParamCount<1 then begin
    writeln('Examples:HexView TextFilename');
    exit;
  end;
  if not FileExists(ParamStr(1)) then begin
    writeln('The file:',ParamStr(1),'not found!');
    exit;
  end;
  //tmpStr:=GetMemory(72);
  msSrc:=TMemoryStream.Create;
  try
    mssrc.LoadFromFile(ParamStr(1));
    for i:=0 to msSrc.size-1 do begin
      tmpStr:=tmpStr+ByteToHex(byte(pchar(msSrc.memory)[i]))+' ';
      if (pchar(msSrc.memory)[i] in dispChar) then
        ascstr:=ascstr+pchar(msSrc.memory)[i]
      else
        ascstr:=ascstr+nonDispChar;
      if ((i mod 8)=7) and ((i mod 16)<>15)then
        tmpStr:=tmpStr+modSp;
      m:=i mod 16;
      if (m=15) or (i=msSrc.size-1) then begin
        tmpstr:=tmpstr+stringofchar(' ',(15-m)*3);
        if m<7 then tmpStr:=tmpStr+'  ';
        tmpStr:=tmpStr+'   '+ascStr+#10#13;
        ascStr:='';
        write(tmpstr);
        tmpStr:='';
      end;
    end;
  finally
    msSrc.free;
    //FreeMemory(tmpStr);
  end;
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值