1

I want translate code to assembly, but i don't understand , how i can get data from string ( or bytes) Example String: 123 --> uint 123

for (uint256 i = 0; i < bytes(_str).length; i++) 
    {
        if ((uint8(bytes(_str)[i]) - 48) < 0 || (uint8(bytes(_str)[i]) - 48) > 9) 
        {
            return (0, false);
        }
        res = res + (uint8(bytes(_str)[i]) - 48) * (10**(_str.length - i - 1));
    }

My try:

bytes memory stringNumber = bytes(_str);
    assembly
    {
        
        function power(level,num) -> result
        {
            let i:=0x1
            result := num
            if gt(level,0)
            {
                for {} lt(i,level) {}
                {
                    result := mul(result,num)
                    i:=add(i,0x1)
                }
            }
            if eq(level,0)
            {
                result := 0x1
            }
            
        }

        let lengthStr := mload(stringNumber) 
        let data := mload(add(stringNumber,0x20))  
        let temp
        
        let  level10 
        for {let i:=0 } 
            lt(i,lengthStr)
            {i := add(i,0x1)}
            {
                temp := sub(data, 0x30) 
                if or(lt(temp,0x0),gt(temp,0x9))
                {
                    res := 0x0
                    stop()
                }
                level10 := power(sub(sub(lengthStr,i),0x1)  ,0xA) //true
                res := add(res, mul(temp,level10))
                data := mload(add(stringNumber,i)))  
            }
       /* try #2
        for {let end := add(data, mul(lengthStr,0x20)) } 
            lt(data,end)
            {data := add(data,0x20)}
            {
                temp := sub(data, 0x30)
                if or(lt(temp,0x0),gt(temp,0x9))
                {
                    res := 0x0
                    stop()
                }
                level10 := power(sub(lengthStr,sub(ind,0x1))  ,0xA)
                res := 1//add(res, mul(temp,level10))
                ind := add(ind,0x1)
            }*/

    }

I'm beginner, don't hit me strong, please

1 Answer 1

0

Solution:

function stringToUint256(string memory _str) internal pure returns(uint256 res) 
{
    bytes memory stringNumber = bytes(_str);
    for (uint256 i = 0; i < bytes(_str).length; i++) 
    {
        uint8 codeSymbol = uint8(stringNumber[i]) - 48; 
        if (codeSymbol < 0 || codeSymbol > 9) 
        {
            return 0;
        }
        assembly
        {

            let len := mload(stringNumber)
            let level10 := exp(0xA,sub(sub(len,i),1))
            res := add(res, mul(codeSymbol,level10))
        }
    }

    return res;
} 
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 1, 2023 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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