Recent

Author Topic: Save any type of variable to array of byte  (Read 3788 times)

bastla

  • New Member
  • *
  • Posts: 23
Save any type of variable to array of byte
« on: November 25, 2013, 10:37:47 pm »
Hello together,

as I understand variables, they're nothing else than bytes. So I had that awsome idea to store any value into an array of bytes.

I created a class named "TValue" and it contains the array of byte named "value". This is how I store variables to the array of bytes:
Code: [Select]
procedure TValue.store(const any);
begin
  SetLength(self.value, 0);
  SetLength(self.value, SizeOf(any));
  Move(any, self.value, SizeOf(any));
end;

My function to convert this bytes back to an integer looks like this:
Code: [Select]
function TValue.Integer: integer;
begin
   Move(self.value, Result, SizeOf(value));
end;

But this doesn't work, I get "0" as output, whatever integer variable I use for "any"...
I actually do not know what I'm doing wrong, I would appreciate your help!

Thanks in advance and greetings,
bastla

ausdigi

  • Jr. Member
  • **
  • Posts: 52
  • Programmer
    • RNR
Re: Save any type of variable to array of byte
« Reply #1 on: November 26, 2013, 01:12:10 am »
You are actually changing the array variable pointer rather than its elements - you need to pass in its first element. The compiler was thinking you wanted to copy the address of the array rather than the array. Looking at the changes happen in the debugger might have helped. Check my modifications below.

Code: [Select]
procedure TValue.store(const any);
begin
  //superfluous SetLength(self.value, 0);
  SetLength(self.value, SizeOf(any));
  Move(any, self.value[0], SizeOf(any));
end;

function TValue.Integer: integer;
begin
   Move(self.value[0], Result, SizeOf(value));
end;
Win10/64: CT 5.7 32&64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Save any type of variable to array of byte
« Reply #2 on: November 26, 2013, 03:30:19 am »
Regarding SizeOf, I don't think it works. According to its documentation:

Quote
...Its result is calculated at compile-time, and hard-coded in the executable.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Save any type of variable to array of byte
« Reply #3 on: November 26, 2013, 05:18:54 am »
Well lets start by the the fact the parameter any is an untyped parameter untyped parameters have no size and you can not access them directly. The only thing that works on them is the address operator or passing them to other function that accepts untyped parameters. there for sizeof should always be 0 in your case and it should not return anything that is not there when you call the method.
References.
http://www.freepascal.org/docs-html/ref/refsu59.html#x158-16800014.4.2
http://forum.lazarus.freepascal.org/index.php/topic,16434.msg89190.html#msg89190
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Save any type of variable to array of byte
« Reply #4 on: November 26, 2013, 03:33:59 pm »
Seems to me you're trying to reinvent the wheel, which is already implemented in the compiler and RTL, and can be utilised with simple declarations of this sort:
Code: [Select]
var
 v:variant;

sam707

  • Guest
Re: Save any type of variable to array of byte
« Reply #5 on: November 26, 2013, 05:40:04 pm »
@howardpc

I agree : using variants plus TMemoryStream is the original wheel that sounds to be reinvented here

 

TinyPortal © 2005-2018