Date: Mon, 15 Oct 2001 21:35:23 +0200 Newsgroups: borland.public.delphi.objectpascal Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Newsreader: Virtual Access by Atlantic Coast PLC, http://www.atlantic-coast.com/va Organization: TeamB Message-ID: Subject: Re: Reading integers from binary file From: "Peter Below (TeamB)" <100113.1101@compuXXserve.com> Reply-To: 100113.1101@compuXXserve.com References: <3bc9bf92_1@dnews> NNTP-Posting-Host: 172.179.53.177 X-Trace: dnews 1003174385 172.179.53.177 (15 Oct 2001 12:33:05 -0700) Lines: 89 Path: dnews Xref: dnews borland.public.delphi.objectpascal:210064 In article <3bc9bf92_1@dnews>, Artem wrote: > 1) I have a binary file of 32-bit unsigned values. However the order of > bytes is reversed: > > 00000000: 00 1C 08 BB 00 00 00 0A 00 00 00 D1 00 00 00 D4 > > If I were to read them, I would get $BB081C00, $A000000, $D1000000 and > $D4000000. What i want to get is $1C08BB, $A, $D1, $D4. > > How do I go about it? Do I use wrong type (LongWord) when reading from the > stream? No, the file was written on a computer that uses big-endian byte order, while DOS/Windows uses little-endian. YOu have to reverse the order of bytes in a LongWord after you have read it. Function Swap32( aLong: LongWord ): LongWord: Assembler; Asm BSWAP eax End; That performs the byte swap. If you want to convert the file itself the following example may come in handy: Procedure Swap32( p: Pointer; counter: DWORD ); Assembler; Asm push edi mov edi, eax // pointer mov ecx, edx // counter @loop: mov eax, [edi] BSWAP eax stosd dec ecx jnz @loop pop edi End; procedure TForm1.Button2Click(Sender: TObject); var fs: TFilestream; hMap: Thandle; pMap: Pointer; pDW : ^DWORD; i: Integer; start: Longword; begin start := gettickcount; screen.cursor := crHourglass; fs:= TFilestream.Create( filename, fmOpenreadwrite or fmShareExclusive ); try hMap := CreateFileMapping( fs.Handle, Nil, PAGE_READWRITE, 0, fs.Size, nil ); if hMap <> 0 then begin pMap := MapViewOfFile( hMap, FILE_MAP_READ or FILE_MAP_WRITE, 0, 0, fs.Size ); if pMap <> Nil then begin pDW := pMap; try Swap32( pDW, fs.size div 4 ); finally UnmapViewOfFile( pMap ); CloseHandle( hMap ); end; end else CloseHandle( hMap ); end; finally fs.free; screen.cursor := crDefault; end; ShowMessage( format('Ticks used: %d', [gettickcount-start])); end; Peter Below (TeamB) 100113.1101@compuserve.com) No e-mail responses, please, unless explicitly requested! Note: I'm unable to visit the newsgroups every day at the moment, so be patient if you don't get a reply immediately.