2

I currently have a situation where I have variables like this:

uint8 _variable1 = 4;
uint8 _variable2 = 2;
uint8 _variable3 = 99;
uint8 _variable4 = 1;
uint8 _variable5 = 0;
uint8 _variable6 = 2;
uint8 _variable7 = 1;
uint8 _variable8 = 34;

When I have to pass or return these values (since Solidity doesn't report Structs as parameters or return values) I keep running into this issue:

CompilerError: Stack too deep, try removing local variables.

The usage of the function to retrieve these values looks like this:

uint8 _variable1;
uint8 _variable2;
uint8 _variable3;
uint8 _variable4;
uint8 _variable5;
uint8 _variable6;
uint8 _variable7;
uint8 _variable8;

(_variable1, _variable2, _variable3, _variable4, _variable5, _variable6) = dataContract.getData(_id);

What I would like to do instead is store a single variable such as uint64 data = 0402990100020134

How would I correctly define that in solidity?

If I wanted to get the value of variable 6, how would I do that?

  • If funciton getData outside of your contract? – Roman Frolov Jan 31 '18 at 10:56
  • Yes, getData will be called from an external contract – Nanolucas Jan 31 '18 at 10:57
  • 2
    why not returning an array ? – salanfe Jan 31 '18 at 12:17
  • For some reason I didn't think that would help with the 'stack too deep' error, but apparently I was overthinking it. After refactoring into using an array it seems to be compiling now. If you make this reply as an answer instead of a comment I can accept it as the answer. – Nanolucas Feb 2 '18 at 0:32
2

You can use bit shift operators to pack data from smaller integers to bigger integers.

E.g.

uint8 a = 1, b = 2;
uint16 foo = (b << 16) | a;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.