0

I want to exract n bytes from a given bytes data.

 //this function extract the first uint8 from a byte data
function extractSelector (bytes data) public constant returns (uint8) {
        uint8 sel = uint8(data[0]);
        return sel;
    }

and the second will extract a web3 signature (65 bytes) from the rest of bytes

 function extractSig (bytes data, uint8 from, uint8 n) public constant returns(bytes) {
      bytes memory returnValue = new bytes(n);
      for (uint8 i = from; i < n; i++) {
        returnValue[i] = data[i];
      }
      return returnValue;
    }

I have this data:

data = "0x0436ee02530753fcaecf730e793c2e66791723c6c6a52e385d8200f2dfc1406a6325bba49a98373c9793fa07f82f69c7a1701cbc0cb379b957d5e96d398f1ca1e600"

//extractSelector(data) retruns 4 => it's okay!

but:

extractSig(data,1,65) returns "0x0036ee02530753fcaecf730e793c2e66791723c6c6a52e385d8200f2dfc1406a6325bba49a98373c9793fa07f82f69c7a1701cbc0cb379b957d5e96d398f1ca1e6"

//it seems like the two last "0" jump at the bigining of bytes. 

what shall I do to conserve the same data ?

1

You were filling returnValue from position from (in your case 1) and that caused the offset of zeroes at th ebeginning of your result. You need to either :

  • start the loop from 0 and add the offset in the data like data[i + from] function extractSig (bytes data, uint8 from, uint8 n) public constant returns(bytes) { bytes memory returnValue = new bytes(n); for (uint8 i = 0; i < n - from; i++) { returnValue[i] = data[i + from]; } return returnValue; }
  • or just substract the offset from the return value index (returnValue[i - from]) function extractSig (bytes data, uint8 from, uint8 n) public constant returns(bytes) { bytes memory returnValue = new bytes(n); for (uint8 i = from; i < n; i++) { returnValue[i - from] = data[i + from]; } return returnValue; }

Hope it helps :D

  • It works thanks ! I made a kind of stupid error :p – maroodb Jun 20 '18 at 11:52

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.