如何根据参数中的数组来设置返回数组的固定长度?
// For example I have this simple function, which convert array uint to int
// but this not works, because dynamic length array
function convertUintToInt(uint[] _input) public pure returns(int[] _output){
for(uint i = 0; i < _input.length; i++){
_output[i] = int(_input[i]);
}
return _output;
}
// this works for length 10, but if input.length < 10 this return unnecessary empty items
// if _input.length > 10 this will not works
function convertUintToInt(uint[] _input) public pure returns(int[10] _output)
// I need just something like this
function convertUintToInt(uint[] _input) public pure returns(int[_input.length] _output)
主要目标不使用存储空间!
我只需要创建可读的合同助手来转换数据
答案 0 :(得分:2)
pragma solidity ^0.5.12;
contract Ballot {
function convertUintToInt(uint256[] memory _input) public pure returns(int[] memory){
int[] memory tmpArr = new int[](_input.length);
for(uint i = 0; i < _input.length; i++){
tmpArr[i] = int(_input[i]);
}
return tmpArr;
}
}