Question? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Question?

bpajkbpajk Posts: 156Member
I nead to change any type of variable into an array of byte and back. For now I have it done like so:

[CODE]
procedure changetype(var x,y);
begin
y:=x
end;
[/CODE]

But this only copys the first 4 bytes of the variable x into y.

[CODE]
var x,y:array[0..9]of byte;
i:byte;
*****
for i:=0 to 9 do
x[i]:=i;

changetype(x,y);

for i:=0 to 9 do
writeln(y[i]);
[/CODE]

If x is an array of byte [0,1,2,3,4,5,6,7,8,9], after I change the type, y will be [0,1,2,3,0,0,0,0,0,0].

How would I change any type (record, string, longint, word...) into an array of byte? I use Dev-Pascal.

Comments

  • zibadianzibadian Posts: 6,349Member
    : I nead to change any type of variable into an array of byte and back. For now I have it done like so:
    :
    : [CODE]
    : procedure changetype(var x,y);
    : begin
    : y:=x
    : end;
    : [/CODE]
    :
    : But this only copys the first 4 bytes of the variable x into y.
    :
    : [CODE]
    : var x,y:array[0..9]of byte;
    : i:byte;
    : *****
    : for i:=0 to 9 do
    : x[i]:=i;
    :
    : changetype(x,y);
    :
    : for i:=0 to 9 do
    : writeln(y[i]);
    : [/CODE]
    :
    : If x is an array of byte [0,1,2,3,4,5,6,7,8,9], after I change the type, y will be [0,1,2,3,0,0,0,0,0,0].
    :
    : How would I change any type (record, string, longint, word...) into an array of byte? I use Dev-Pascal.
    :
    Here is a sample code, which might give you the correct results.
    [code]
    type
    TByteArray = array of byte;

    function ChangeType(const Data): TByteArray;
    begin
    SetLength(Result, SizeOf(Data)); // Create a long enough array
    Move(Data, Result, SizeOf(Data)); // Copy the data into the array
    end;
    [/code]
    I don't use Dev-Pascal myself, so I'm not sure if the syntax is correct. The most important part of this function is the Move() call, which I think Dev-Pascal should recognize (TP6 through Delphi 5 recognize it).
  • bpajkbpajk Posts: 156Member
    Thanx! The move command works fine:)
Sign In or Register to comment.