Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Question? Posted by bpajk on 29 Sept 2003 at 1:20 AM
I nead to change any type of variable into an array of byte and back. For now I have it done like so:

procedure changetype(var x,y);
begin
  y:=x
end;


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

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]);


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.
Report
Re: Question? Posted by zibadian on 29 Sept 2003 at 3:40 AM
: I nead to change any type of variable into an array of byte and back. For now I have it done like so:
:
:
: procedure changetype(var x,y);
: begin
:   y:=x
: end;
: 

:
: But this only copys the first 4 bytes of the variable x into y.
:
:
: 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]);
: 

:
: 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.
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;

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).
Report
Re: Question? Posted by bpajk on 29 Sept 2003 at 12:40 PM
Thanx! The move command works fine:)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.