Question? - Programmers Heaven

#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Question?

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.

• 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).
• Posts: 156Member
Thanx! The move command works fine:)