program dp_185013;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Classes,
System.NetEncoding,
System.SysUtils;
type
TMyData =
packed record
Header: Byte;
Amount: Byte;
Data: Int32;
Tail: Byte;
end;
procedure OutputBuffer( ABuffer: TBytes );
var
I: Integer;
begin
for I := low( ABuffer )
to high( ABuffer )
do
begin
write( IntToHex( ABuffer[ I ], 2 ), '
' );
if ( I + 1 - low( ABuffer ) )
mod 16 = 0
then
Writeln;
end;
Writeln;
end;
procedure TestData;
var
LData: TMyData;
LBuffer: TBytes;
begin
// Daten in den Record schreiben
LData.Header := $02;
LData.Amount := StrToInt( '
1' );
LData.Data := StrToInt( '
46762' );
LData.Tail := 0;
// Buffer auf Mass bringen
SetLength( LBuffer, SizeOf( TMyData ) );
// Record in einem Rutsch in den Buffer kopieren
Move( LData, LBuffer[ 0 ], Length( LBuffer ) );
// Ausgabe als HEX
OutputBuffer( LBuffer );
// Ausgabe als Base64
Writeln( TNetEncoding.Base64.EncodeBytesToString( LBuffer ) );
end;
procedure Test;
var
LStream: TMemoryStream;
LBuffer: TBytes;
begin
LStream := TMemoryStream.Create;
try
// Daten in den Stream
LStream.WriteData( $02 );
LStream.WriteData( Byte( StrToInt( '
1' ) ) );
LStream.WriteData( Int32( StrToInt( '
46762' ) ) );
LStream.WriteData( $00 );
// Stream-Inhalt nach TBytes kopieren
SetLength( LBuffer, LStream.Size );
LStream.Position := 0;
LStream.
Read( LBuffer, Length( LBuffer ) );
// Ausgabe der TBytes zur Kontrolle
OutputBuffer( LBuffer );
Writeln( TNetEncoding.Base64.EncodeBytesToString( LBuffer ) );
finally
LStream.Free;
end;
end;
begin
try
Test;
TestData;
except
on E:
Exception do
Writeln( E.ClassName, '
: ', E.
Message );
end;
ReadLn;
end.