Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The uint32 writer implementation of SCALE has problem #81

Open
LBruyne opened this issue Dec 6, 2021 · 1 comment
Open

The uint32 writer implementation of SCALE has problem #81

LBruyne opened this issue Dec 6, 2021 · 1 comment

Comments

@LBruyne
Copy link

LBruyne commented Dec 6, 2021

write method of UInt32Writer should has a param of type Long, not integer.
The current version will have a problem when encoding uint32 like 2282200012 (which is beyond the limit of integer).

@LBruyne
Copy link
Author

LBruyne commented Dec 6, 2021

the implementation can be:

public class UInt32Writer implements ScaleWriter<Long> {

    private static final long MAX_UINT32 = Long.parseLong("4294967295");

    @Override
    public void write(ScaleCodecWriter wrt, Long value) throws IOException {
        if (value < 0 || value > MAX_UINT32) {
            throw new IllegalArgumentException("Only values in range 0.." + MAX_UINT32 + " are supported: " + value);
        }
        for( int i = 0; i < 4; i++) {
            wrt.directWrite(Math.toIntExact((value >> (i * 8)) & 0xff));
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant