Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2009-01

Loading an integer into a byte array

13 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Dave Coventry

I have a file into which I want to put a long integer:

00 00 19 7A

The file expects the value in the form:

buffer[0]:=122;
buffer[1]:=25;
buffer[2]:=0;
buffer[3]:=0;

I am then intending to write this buffer to the file

FS.Writebuffer(buffer,4);

Is there a way of loading the buffer directly before writing?

Reply by email

Bernd Mueller
to Dave Coventry
Dave Coventry wrote 16 lines
I have a file into which I want to put a long integer: 00 00 19 7A The file expects the value in the form: buffer[0]:=122; buffer[1]:=25; buffer[2]:=0; buffer[3]:=0; I am then intending to write this buffer to the file FS.Writebuffer(buffer,4); Is there a way of loading the buffer directly before writing?

You could use the Absolute directive to map a longint variable to your buffer. Not pretty, but fast ;-)

procedure LoadBuffer;
var
    Buffer: Array[0..127] of Byte;
    l: Longint Absolute Buffer;
begin
    l:= 6522;
end;

Regards, Bernd.

Reply by email

Hans-Peter Diettrich
to Dave Coventry: I have a file into which I want to put a long integer: 00 00 19 7A The file expects the…
Show quoted text 5 lines
I have a file into which I want to put a long integer: 00 00 19 7A The file expects the value in the form:

Which file?

buffer[0]:=122; buffer[1]:=25; buffer[2]:=0; buffer[3]:=0;

That's the Intel byte order.

Show quoted text 5 lines
I am then intending to write this buffer to the file FS.Writebuffer(buffer,4); Is there a way of loading the buffer directly before writing?

What is "loading"? Reading from the file?

Otherwise buffer can be a variable of any suitable type, e.g. integer.

DoDi

Reply by email

Dave Coventry
to Bernd Mueller: You could use the Absolute directive to map a longint variable to your buffer. Not…

Thanks Bernd.

Won't this mean a different file on big endian and little endian machines?

2009/1/8 Bernd Mueller :

Show quoted text 35 lines
Dave Coventry wrote: > I have a file into which I want to put a long integer: > > 00 00 19 7A > > The file expects the value in the form: > > buffer[0]:=122; > buffer[1]:=25; > buffer[2]:=0; > buffer[3]:=0; > > I am then intending to write this buffer to the file > > FS.Writebuffer(buffer,4); > > Is there a way of loading the buffer directly before writing? You could use the Absolute directive to map a longint variable to your buffer. Not pretty, but fast ;-) procedure LoadBuffer; var Buffer: Array[0..127] of Byte; l: Longint Absolute Buffer; begin l:= 6522; end; Regards, Bernd. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Andrew Brunner
to Dave Coventry: I have a file into which I want to put a long integer: 00 00 19 7A The file expects the…

System.Move()

Dave Coventry wrote 21 lines
I have a file into which I want to put a long integer: 00 00 19 7A The file expects the value in the form: buffer[0]:=122; buffer[1]:=25; buffer[2]:=0; buffer[3]:=0; I am then intending to write this buffer to the file FS.Writebuffer(buffer,4); Is there a way of loading the buffer directly before writing? _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Martin Friebe
to Bernd Mueller: You could use the Absolute directive to map a longint variable to your buffer. Not…
Bernd Mueller wrote 31 lines
Dave Coventry wrote: > I have a file into which I want to put a long integer: > > 00 00 19 7A > > The file expects the value in the form: > > buffer[0]:=122; > buffer[1]:=25; > buffer[2]:=0; > buffer[3]:=0; > > I am then intending to write this buffer to the file > > FS.Writebuffer(buffer,4); > > Is there a way of loading the buffer directly before writing? > You could use the Absolute directive to map a longint variable to your buffer. Not pretty, but fast ;-) procedure LoadBuffer; var Buffer: Array[0..127] of Byte; l: Longint Absolute Buffer; begin l:= 6522; end;
Or you can use pointer operation. Someting like
type
  PLongint = ^Longint;
PLongint(@Buffer[0])^ = 6522;

But be aware: Neither this nor Bernd's way are portable. The order of byte will depend on the architecture your application runs on.

Reply by email

Joost van der Sluis
to Dave Coventry: I have a file into which I want to put a long integer: 00 00 19 7A The file expects the…

Op donderdag 08-01-2009 om 16:05 uur [tijdzone +0200], schreef Dave Coventry:

Show quoted text 16 lines
I have a file into which I want to put a long integer: 00 00 19 7A The file expects the value in the form: buffer[0]:=122; buffer[1]:=25; buffer[2]:=0; buffer[3]:=0; I am then intending to write this buffer to the file FS.Writebuffer(buffer,4); Is there a way of loading the buffer directly before writing?
var l : longint;
l := NtoBE($197a); // Number to BigEndian
Fs.WriteInt(l);

I don't have tried it, but it must be something like that?

Joost.

Reply by email

Dave Coventry
to Hans-Peter Diettrich: Which file? That's the Intel byte order. What is "loading"? Reading from the file?…

Thanks DoDi.

2009/1/8 Hans-Peter Diettrich :

Dave Coventry schrieb: Which file?

It's just a data file that I'm writing to. But it does have to be portable to other applications.

That's the Intel byte order.

Yes, the specification of the datafile require little endian byte order.

What is "loading"? Reading from the file?

No, the integer value is calculated and must be saved to the file. There is more than one integer in a 48 byte block and I want to write this block to the file, so I'm loading a 48 byte buffer. Some of the values are single bytes or text.

Reply by email

Dave Coventry
to Dave Coventry

It sounds as though the best option is to write my own function:

procedure loadValues(var buffer: Array of Byte; value, startPos,
finPos: integer);
var i: integer;
begin
 for i:=startPos to finPos do
 begin
 buffer[i]:=value and 255;
 value:=value shr 8;
 end;
end;

Reply by email

Hans-Peter Diettrich
to Dave Coventry: Thanks DoDi. 2009/1/8 Hans-Peter Diettrich : It's just a data file that I'm writing to.…
Show quoted text 5 lines
> What is "loading"? Reading from the file? No, the integer value is calculated and must be saved to the file. There is more than one integer in a 48 byte block and I want to write this block to the file, so I'm loading a 48 byte buffer. Some of the values are single bytes or text.

You can define an according record, and fill in the values there. When the byte order has to be changed, use the according system routines.

DoDi

Reply by email

Duncan Parsons
to Joost van der Sluis: Op donderdag 08-01-2009 om 16:05 uur [tijdzone +0200], schreef Dave Coventry: var l :…

Must confess, Joost's seemed the most readable, simple and portable :-)

DSP

Joost van der Sluis wrote 32 lines
Op donderdag 08-01-2009 om 16:05 uur [tijdzone +0200], schreef Dave Coventry: > I have a file into which I want to put a long integer: > > 00 00 19 7A > > The file expects the value in the form: > > buffer[0]:=122; > buffer[1]:=25; > buffer[2]:=0; > buffer[3]:=0; > > I am then intending to write this buffer to the file > > FS.Writebuffer(buffer,4); > > Is there a way of loading the buffer directly before writing? var l : longint; l := NtoBE($197a); // Number to BigEndian Fs.WriteInt(l); I don't have tried it, but it must be something like that? Joost. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Bernd Mueller
to Duncan Parsons
Duncan Parsons wrote
Must confess, Joost's seemed the most readable, simple and portable :-)

but consider NtoLE, if you are dealing mostly with Little Endian machines. Should be faster then.

Regards, Bernd.

Reply by email

Benito van der Zander

Wouldn't it be a better much nicer way, if something like this were possible:

var l : longintBE;
l := $197a;
Fs.WriteInt(l);

This should just need a new type, operator overloading and an overloaded writeint.

Or adding a new method WriteIntBE is also interesting;

Benito

Reply by email