Skip to main content
deleted 9 characters in body
Source Link

I've written better code to handlerhandle bytes convert with offset and length support. The good point is, It's keep bytes order.

/**
* Convert bytes to uint
* @param _data bytes Byte array
* @param _offset uint256 Position to convert 
* @param _length uint256 Data length
*/
function toUint(bytes _data, uint256 _offset, uint256 _length)
internal pure
returns(uint256 _result) {
    require(_offset >= 0);
    require(_length > 0);
    require((_offset + _length) <= _data.length);
    uint256 _segment = _offset + _length;
    uint256 count = 0;
    for (uint256 i = _segment; i > _offset ; i--) {
        _result |= uint256(_data[i-1]) << ((count++)*8);
    }
}

I've written better code to handler bytes convert with offset and length support. The good point is, It's keep bytes order.

/**
* Convert bytes to uint
* @param _data bytes Byte array
* @param _offset uint256 Position to convert 
* @param _length uint256 Data length
*/
function toUint(bytes _data, uint256 _offset, uint256 _length)
internal pure
returns(uint256 _result) {
    require(_offset >= 0);
    require(_length > 0);
    require((_offset + _length) <= _data.length);
    uint256 _segment = _offset + _length;
    uint256 count = 0;
    for (uint256 i = _segment; i > _offset ; i--) {
        _result |= uint256(_data[i-1]) << ((count++)*8);
    }
}

I've written better code to handle bytes convert with offset and length. The good point is, It's keep bytes order.

/**
* Convert bytes to uint
* @param _data bytes Byte array
* @param _offset uint256 Position to convert 
* @param _length uint256 Data length
*/
function toUint(bytes _data, uint256 _offset, uint256 _length)
internal pure
returns(uint256 _result) {
    require(_offset >= 0);
    require(_length > 0);
    require((_offset + _length) <= _data.length);
    uint256 _segment = _offset + _length;
    uint256 count = 0;
    for (uint256 i = _segment; i > _offset ; i--) {
        _result |= uint256(_data[i-1]) << ((count++)*8);
    }
}
Source Link

I've written better code to handler bytes convert with offset and length support. The good point is, It's keep bytes order.

/**
* Convert bytes to uint
* @param _data bytes Byte array
* @param _offset uint256 Position to convert 
* @param _length uint256 Data length
*/
function toUint(bytes _data, uint256 _offset, uint256 _length)
internal pure
returns(uint256 _result) {
    require(_offset >= 0);
    require(_length > 0);
    require((_offset + _length) <= _data.length);
    uint256 _segment = _offset + _length;
    uint256 count = 0;
    for (uint256 i = _segment; i > _offset ; i--) {
        _result |= uint256(_data[i-1]) << ((count++)*8);
    }
}