Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Leer Word, Integer... de un Buffer de bytes (https://www.clubdelphi.com/foros/showthread.php?t=55503)

vejerf 18-04-2008 10:55:00

Leer Word, Integer... de un Buffer de bytes
 
Hola amig@s,
Me ha surgido la siguiente duda al implementar un protocolo. En este protocolo me llega una ristra de bytes en formato hexadecimal por el puerto serie. Yo los voy almacenando hasta tener una trama completa y cuando me dispongo a desempaquetarlo tengo q leer en un caso un byte, en otro un Word, o caracteres, etc...
Mi pregunta y mi duda es si hay algún método sencillo para leer de mi buffer un tipo de dato en concreto o voy a tener q hacer transformaciones del tipo:

Word := Buffer[1]*2^8+Buffer[2];

Gracias a todos...

duilioisola 18-04-2008 11:35:53

En el Help de Delphi 6
Overview of Pointers

Código Delphi [-]
var
  X, Y: Integer;   // X and Y are Integer variables
  P: ^Integer;     // P points to an Integer
begin
  X := 17;         // assign a value to X
  P := @X;         // assign the address of X to P
  Y := P^;         // dereference P; assign the result to Y
end;

Código Delphi [-]
type
  PInteger = ^Integer;
var
  R: Single;
  I: Integer;
  P: Pointer;
  PI: PInteger;
begin
  ...
  P := @R;
  PI := PInteger(P);
  I := PI^;
end;

En tu caso:

Código Delphi [-]
var
  i : integer;
  PB: ^Byte;
  PI: ^Integer;
  PW: ^Word;
  vByte : Byte;
  vInteger : Integer;
  vWord : Word;
begin 
   while not fin_de_buffer
   begin 
      inc(i);
      if (viene_un_word) then
      begin
         PW := @Buffer[i];
         vWord := PW^;
         i := i + 2;
      end;
      if (viene_un_integer) then
      begin
         PI := @Buffer[i];
         vInteger := PI^;
         i := i + 2;
      end;
      if (viene_un_byte) then
      begin
         PB := @Buffer[i];
         vByte := PB^;
         i := i + 1;
      end;
      ...


La franja horaria es GMT +2. Ahora son las 08:48:56.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi