文件的十六进制视图
我正在使用 Delphi 2009。
我想查看备忘录中文件的内容(十六进制)。
我正在使用这段代码:
var
Buffer:String;
begin
Buffer := '';
AssignFile(sF,Source); //Assign file
Reset(sF);
repeat
Readln(sF,Buffer); //Load every line to a string.
TempChar:=StrToHex(Buffer); //Convert to Hex using the function
...
until EOF(sF);
end;
function StrToHex(AStr: string): string;
var
I ,Len: Integer;
s: chr (0)..255;
//s:byte;
//s: char;
begin
len:=length(AStr);
Result:='';
for i:=1 to len do
begin
s:=AStr[i];
//The problem is here. Ord(s) is giving false values (251 instead of 255)
//And in general the output differs from a professional hex editor.
Result:=Result +' '+IntToHex(Ord(s),2)+'('+IntToStr(Ord(s))+')';
end;
Delete(Result,1,1);
end;
当我将变量“s”声明为char(我知道 char 最大为 255)时,我得到的结果十六进制值高达 65535!
当我将变量“s”声明为byte或chr (0)..255时,与任何十六进制编辑器相比,它输出不同的十六进制值!
这是为什么?我怎样才能看到正确的值?
检查图像是否存在差异。
第一张图片:专业十六进制编辑器。
第二张图片:函数输出到备忘录。
谢谢。
I am using Delphi 2009.
I want to view the contents of a file (in hexadecimal) inside a memo.
I'm using this code :
var
Buffer:String;
begin
Buffer := '';
AssignFile(sF,Source); //Assign file
Reset(sF);
repeat
Readln(sF,Buffer); //Load every line to a string.
TempChar:=StrToHex(Buffer); //Convert to Hex using the function
...
until EOF(sF);
end;
function StrToHex(AStr: string): string;
var
I ,Len: Integer;
s: chr (0)..255;
//s:byte;
//s: char;
begin
len:=length(AStr);
Result:='';
for i:=1 to len do
begin
s:=AStr[i];
//The problem is here. Ord(s) is giving false values (251 instead of 255)
//And in general the output differs from a professional hex editor.
Result:=Result +' '+IntToHex(Ord(s),2)+'('+IntToStr(Ord(s))+')';
end;
Delete(Result,1,1);
end;
When I declare variable "s" as char (i know that char goes up to 255) I get results hex values up to 65535!
When i declare variable "s" as byte or chr (0)..255, it outputs different hex values, comparing to any Hexadecimal Editor!
Why is that? How can I see the correct values?
Check images for the differences.
1st image: Professional Hex Editor.
2nd image: Function output to Memo.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的 Delphi 2009 启用了 unicode,因此
Char
实际上是WideChar
,这是一个 2 字节、16 位无符号值,可以具有从 0 到 65535 的值。您可以更改将所有
Char
声明转换为AnsiChar
并将所有String
声明转换为AnsiString
,但这不是这样做的方法。您应该放弃 Pascal I/O,转而使用基于流的现代 I/O,使用TFileStream
,并且不要将二进制数据视为Char
。控制台演示:
Your Delphi 2009 is unicode-enabled, so
Char
is actuallyWideChar
and that's a 2 byte, 16 bit unsigned value, that can have values from 0 to 65535.You could change all your
Char
declarations toAnsiChar
and all yourString
declarations toAnsiString
, but that's not the way to do it. You should drop Pascal I/O in favor of modern stream-based I/O, use aTFileStream
, and don't treat binary data asChar
.Console demo:
在 Delphi 2009 中,
Char
与WideChar
相同,即 Unicode 字符。一个宽字符占用两个字节。您想要使用AnsiChar
。在 Delphi 2009 之前(即在 Unicode Delphi 之前),Char
与AnsiChar
相同。另外,您不应该使用
ReadLn
。您将该文件视为具有文本文件行结尾的文本文件!这是一个通用文件!它可能根本没有任何文本文件行结尾!In Delphi 2009, a
Char
is the same thing as aWideChar
, that is, a Unicode character. A wide character occupies two bytes. You want to useAnsiChar
. Prior to Delphi 2009 (that is, prior to Unicode Delphi),Char
was the same thing asAnsiChar
.Also, you shouldn't use
ReadLn
. You are treating the file as a text file with text-file line endings! This is a general file! It might not have any text-file line endings at all!为了更容易阅读输出并且看起来更好,您可能需要使用这个简单的十六进制转储格式化程序。
HexDump 过程将内存区域转储到 TStrings 中,每行两块,每行 8 字节(十六进制)和 16 个 ascii 字符
示例
以下是转储格式函数的代码
这是转储文件内容的完整代码示例进入备注字段。
For an easier to read output, and looking better too, you might want to use this simple hex dump formatter.
The HexDump procedure dumps an area of memory into a TStrings in lines of two chunks of 8 bytes in hex, and 16 ascii chars
example
Here is the code for the dump format function
And here is a complete code example for dumping the contents of a file into a Memo field.
string
在 Delphi 2009 中是UnicodeString
。如果要使用单字节字符串,请使用AnsiString
或RawByteString
。请参阅字符串类型。
string
isUnicodeString
in Delphi 2009. If you want to use single-byte strings useAnsiString
orRawByteString
.See String types.