Hello All,
I am trying to code a tool which will detect the files on the basis of its Header value(Magic number), and not File extension or MIME type.
however, I am unable to read the first 4-bytes using VB.NET
I tried StreamReader/BinaryReader for the same however it starts at offset 0, and it fetches entire file eof (-1).
and i just need the first 4-bytes only.
if anyone experienced in coding please help me with a small piece of code.
Thanks.
I don't code in VB, but in Pascal you need to declare what you intend to read in. Since you want the first four bytes this would equate to a 32bit integer and this would be the variable you want to read in.
In this instance
var
HeaderInteger;
Load your file into a memory or file stream. The standard header for a jpeg would be FFD8FFE0, but what you actually want to read in is the integer value which is 3774863615.
Go to the first byte in the file which would be file.position = file.size-file.size
Then read in the first four bytes
file.read(header,Sizeof(header));
if header = 3774863615 then
show message('This is a JPG header);
Hope this helps.
See Fileopen and read the part about random access and the recordlength parameter (set it to 4)
Then see the example on this page
Hi,
Assuming you've got the filestream/binary reader part working, then what you need is
'if image is the filestream and br is the binary reader of image
image.Position=0 'set to beginning of file
magic=br.ReadUint32 'read 4 bytes, and move position by 4
'think this gives little-endian format if this is important
Ddan
Hi,
Assuming you've got the filestream/binary reader part working, then what you need is
'if image is the filestream and br is the binary reader of image
image.Position=0 'set to beginning of file
magic=br.ReadUint32 'read 4 bytes, and move position by 4
'think this gives little-endian format if this is important
Ddan