此程序可以将文档转成HEX格式输出。DELPHI7,WINXP编译通过
program phexview;
{$APPTYPE CONSOLE}
uses
SysUtils,classes;
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');
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
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;
{ 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.