Script - true and false values

1

In the Script, when one is checking whether a value is true of false, should the length of the expression be checked? For example, 0x01 is evaluated as true, but can the same be said about 0x0001, 0x000001, etc?

ThePiachu

Posted 2011-12-26T15:20:40.043

Reputation: 41 594

Answers

1

Anything that doesn't encode a form of zero is considered to be true. Here's the code Bitcoin uses:

bool CastToBool(const valtype& vch)
{
    for (int i = 0; i < vch.size(); i++)
    {
        if (vch[i] != 0)
        {
            // Can be negative zero
            if (i == vch.size()-1 && vch[i] == 0x80)
                return false;
            return true;
        }
    }
    return false;
}

David Schwartz

Posted 2011-12-26T15:20:40.043

Reputation: 46 931