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

Fix/plc4j-protocol-ads #286

Merged
merged 1 commit into from Dec 3, 2021

Conversation

rmeister
Copy link
Contributor

Hi,
writing some WORDs with ADS gave me odd results. This is how I've tested my fix:

byte[] convertToByteArrayLE(BigInteger bigIntValue, int targetSize){
	byte[] bytes = bigIntValue.toByteArray();
	ArrayUtils.reverse(bytes);
	return Arrays.copyOf(bytes, targetSize);
}
@Test
void encodeBigInteger(){
	assertArrayEquals(new byte[]{1, 0}, convertToByteArrayLE(BigInteger.valueOf(1), 2)); /* one byte input */
	assertArrayEquals(new byte[]{-128, 0,}, convertToByteArrayLE(BigInteger.valueOf(128), 2)); /* one byte plus sign byte */
	assertArrayEquals(new byte[]{0, 2}, convertToByteArrayLE(BigInteger.valueOf((512)), 2)); /* two bytes, no sign byte */
	assertArrayEquals(new byte[]{-1, -1}, convertToByteArrayLE(BigInteger.valueOf((65535)), 2)); /* end of 16 bit range */
	assertArrayEquals(new byte[]{0, 0}, convertToByteArrayLE(BigInteger.valueOf((65536)), 2)); /* three byte input -> overflow */
	assertArrayEquals(new byte[]{1, 0}, convertToByteArrayLE(BigInteger.valueOf((65537)), 2)); /* three byte input -> overflow */
	assertArrayEquals(new byte[]{1, 0, 1, 0}, convertToByteArrayLE(BigInteger.valueOf((65537)), 4)); /* three bytes, expanded to four */
}

Best regards,
Richard

@rmeister rmeister changed the title plc4j-protocol-ads: fix encodeBigInteger in LittleEndianEncoder Fix/plc4j-protocol-ads Nov 15, 2021
@sruehl
Copy link
Contributor

sruehl commented Dec 3, 2021

Thanks for the PR

@sruehl sruehl merged commit bda80dc into apache:develop Dec 3, 2021
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

Successfully merging this pull request may close these issues.

None yet

2 participants