0

For Bytes32 to 2x int128 I have working code. I've adapted code from this stack Exchange answer

function bytes32to2xint128(bytes32 z) pure public returns (int128 x, int128 y){
    bytes16[2] memory tmp = [bytes16(0), 0];
    assembly {
        mstore(tmp, z)
        mstore(add(tmp, 16), z)
    }
    x=int128(tmp[0]);
    y=int128(tmp[1]);
}

But for the inverse, 2x int128 to Bytes32, the following doesn't work and always returns

bytes32: 0x0000000000000000000000000000000000000000000000000000000000000000

function int128x2tobytes32(int128 x, int128 y) pure public returns (bytes32 z){
    assembly {
        mstore(z, x)
        mstore(add(z, 16), y)
    }
}

Thanks in advance

user1938620
  • 681
  • 1
  • 6
  • 12

1 Answers1

0

Turns out that assembly was not required.

function int128x2tobytes32(int128 x, int128 y) pure public returns (bytes32){
    bytes32 c = bytes16(x);
    bytes32 d = bytes16(y);
    return (c>> 128) | d; 
}

Thanks to this medium article

user1938620
  • 681
  • 1
  • 6
  • 12