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

An 8 bit mask may be needed #21

Open
scuniff opened this issue Mar 15, 2023 · 0 comments
Open

An 8 bit mask may be needed #21

scuniff opened this issue Mar 15, 2023 · 0 comments

Comments

@scuniff
Copy link

scuniff commented Mar 15, 2023

An 8 bit mask may be needed

https://github.com/fiji/IO/blob/1fa709937a44b369c3d56545cea191f0b0b821ed/src/main/java/sc/fiji/io/icns/IOSupport.java

Line 104:
return ((data[0] & 0xFF) << 8) + data[1];

should have an 8 bit mask & operation added:
return ((data[0] & 0xFF) << 8) + (data[1] & 0xff);

This eliminates sign extension issue.

This snippet I think exemplifies the fix:

    byte[] data = new byte[2];
    data[0] = (byte)0x80;
    data[1] = (byte)0x80;

    long incorrect = ((data[0] & 0xff) << 8)  + (data[1]);
	System.out.println("Incorrect " + String.format("0x%08X", incorrect));

	long correct = ((data[0] & 0xff) << 8)  + (data[1] & 0xff);
	System.out.println("correct " + String.format("0x%08X", correct));

Output:

Incorrect 0x00007F80
correct 0x00008080

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