Board index » delphi » FileStream.Write Problem

FileStream.Write Problem

I am writing out an integer to a FileStream but it is writing out backwards
ie

var
  x  : integer;
  fs : TFileStream;

begin
  fs := TFileStream.Create('c:\test.fst');
  x :=75544;
  fs.write(x, 4); //I need it to fill 4 Bytes
  fs.free;
end;

The answer should be     00 01 27 18,
but comes out as            18 27 01 00.

What do need to do to get these bytes in the right order?

TIA

Darrell

TIA

 

Re:FileStream.Write Problem


Quote
Darrell Funk wrote:
> I am writing out an integer to a FileStream but it is writing out backwards
> ie

> var
>   x  : integer;
>   fs : TFileStream;

> begin
>   fs := TFileStream.Create('c:\test.fst');
 >   x :=75544;
>   fs.write(x, 4); //I need it to fill 4 Bytes
>   fs.free;
> end;

> The answer should be     00 01 27 18,
> but comes out as            18 27 01 00.

> What do need to do to get these bytes in the right order?

> TIA

> Darrell

> TIA

This function will put a 32-bit value into the format you need. Change
your code to read

x := MySwap(75544);

Function MySwap(I:Integer):Integer; assembler;
ASM
   BSWAP EAX
END;

Look up BigEndian and/or LittleEndian on google to find out more about
what this is all about.

HTH, Glynn

Re:FileStream.Write Problem


Thanks Glynn

Here are two functions I found on Google to handle this Short(2Byte) And
Long(4Byte).

I appreciate the help,

Darrell
//--------------------------------------------------------------------------
----
function TForm1.EndianInt(i : integer) : integer;
begin
  result := swap(i);
end;

//--------------------------------------------------------------------------
----
function TForm1.EndianLong(L : longint) : longint;
begin
  result := swap(L shr 16) or (longint(swap(L and $ffff)) shl 16);
end;

//--------------------------------------------------------------------------
----

Quote
"Glynn Owen" <gl...@gtstrans.com> wrote in message

news:3D18B602.40708@gtstrans.com...
Quote
> Darrell Funk wrote:
> > I am writing out an integer to a FileStream but it is writing out
backwards
> > ie

> > var
> >   x  : integer;
> >   fs : TFileStream;

> > begin
> >   fs := TFileStream.Create('c:\test.fst');
>  >   x :=75544;
> >   fs.write(x, 4); //I need it to fill 4 Bytes
> >   fs.free;
> > end;

> > The answer should be     00 01 27 18,
> > but comes out as            18 27 01 00.

> > What do need to do to get these bytes in the right order?

> > TIA

> > Darrell

> > TIA

> This function will put a 32-bit value into the format you need. Change
> your code to read

> x := MySwap(75544);

> Function MySwap(I:Integer):Integer; assembler;
> ASM
>    BSWAP EAX
> END;

> Look up BigEndian and/or LittleEndian on google to find out more about
> what this is all about.

> HTH, Glynn

Re:FileStream.Write Problem


Quote
"Darrell Funk" wrote...
> I am writing out an integer to a FileStream but it is writing out
backwards

> The answer should be     00 01 27 18,
> but comes out as            18 27 01 00.

> What do need to do to get these bytes in the right order?

Just out of curiosity: why do you worry about this,
if you read it back the same way you wrote it will
be no problem (Intel compatible processors).

--
Pieter

Re:FileStream.Write Problem


I am merging two software applications and have to write according to a
predefined data format.

Quote
"Pieter Zijlstra" <pzijlstr...@freeler.nl> wrote in message

news:3d18c695_1@dnews...
Quote
> "Darrell Funk" wrote...
> > I am writing out an integer to a FileStream but it is writing out
> backwards

> > The answer should be     00 01 27 18,
> > but comes out as            18 27 01 00.

> > What do need to do to get these bytes in the right order?

> Just out of curiosity: why do you worry about this,
> if you read it back the same way you wrote it will
> be no problem (Intel compatible processors).

> --
> Pieter

Re:FileStream.Write Problem


Quote
In article <3d18be28$1_1@dnews>, "Darrell Funk" <df...@mvinc.net> wrote:
> Here are two functions I found on Google to handle this Short(2Byte) And
> Long(4Byte).

.. well you might have looked in the thread *here* entitled 'datatypes',
where I posted a function to do this, its a bit longer than these, ( and
much longer that Glynn's <g> ), but I thought it made clear just what steps
needed to be taken, (no, I don't get paid by the line <g>).

Re:FileStream.Write Problem


Quote
In article <3d18be28$1_1@dnews>, "Darrell Funk" <df...@mvinc.net> wrote:
> Here are two functions I found on Google to handle this Short(2Byte) And
> Long(4Byte).

.. well you might have looked in the thread *here* entitled 'datatypes',
where I posted a function to do this, its a bit longer than these, ( and
much longer that Glynn's <g> ), but I thought it made clear just what steps
needed to be taken, (no, I don't get paid by the line <g>).

Other Threads