type B02 = array [01..02] of byte ;
...
var b : B02;
...
//here i read from tcp socket
socket.ReadBuffer(b, 2);
The question is: how to convert B02 to an integer?
type B02 = array [01..02] of byte ;
...
var b : B02;
...
//here i read from tcp socket
socket.ReadBuffer(b, 2);
The question is: how to convert B02 to an integer?
My integers are often 4 bytes long. Which part do you want to set?
You could declare a Word/Smallint at the same memory location, like this:
var
b : B02;
myInt: smallint absolute B02;
Then again, is there any particular reason why you don't just create the smallint and pass it to ReadBuffer instead of an array? I don't know exactly what class you're using, but that looks a lot like the way you read from a TStream, and it'll accept variables of any type, along with a size in bytes. Why not just declare your buffer as the integer type you're looking for and cut out the middleman?
If the data is being transmitted in "network" order (highest byte first) and not in "Intel" order (lowest byte first), you can do some byte shufling yourself.
uses
SysUtils;
var
b: B02;
w: word; //two bytes represent a word, not an integer
socket.ReadBuffer(b, 2);
WordRec(w).Hi := b[1];
WordRec(w).Lo := b[2];
Mghie suggested following approach in comments (and I agree with him):
uses Winsock;
var
w: word;
socket.ReadBuffer(w, 2);
w := ntohs(w);
You can just cast it:
var
a: array[01..02] of Byte;
i: Integer;
begin
i := PWORD(@a)^;
end;
or if you need to change the byte order:
i := Swap(PWORD(@a)^);