Can someone help me reproduce this code using the blocks?

public class Utils {
    public static int crc16(byte[] data, int offset, int length) {
        if (data == null || offset < 0 || offset > data.length - 1 || offset + length > data.length) {
            return 0;
        }

        int crc = 0xFFFF;
        for (int i = 0; i < length; ++i) {
            crc ^= data[offset + i] << 8;
            for (int j = 0; j < 8; ++j) {
                crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
            }
        }
        return crc & 0xFFFF;
    }
}
1 Like

What is to be achieved, what is the purpose of this code?

2 Likes

Topic closed in Kodular's community. Please ask only in one community

3 Likes

Crc16 ccitt false

1 Like

it's easier to use code writen in javascript.
Can you find other code in javascript?

1 Like

For Bitwise operarions, I can't suggest any Blocks.

But you may go to Niotron IDE and paste your java code there, compile the extension and import it in your project. Just a suggestion.

1.Save this code as crc16.html.

<script>
const crctab16 = new Uint16Array([
		0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
	]);
var crc16 = function (data) {
	var res = 0xffff;
	for (let b of data) {
		res = crctab16[(b ^ res) & 15] ^ (res >> 4);
		res = crctab16[((b >> 4) ^ res) & 15] ^ (res >> 4);
	}
	window.AppInventor.setWebViewString ((res) & 0xffff);
	
};
</script>

  1. upload it to assets
  2. make blocks like this:
    image
1 Like

If you still want just blocks, see

I think this code is different because the result I expected was different, I have a code of the same crc16 in javascript can you teach me how to do the same as you did in this one?

function crc16(data, offset, length) {
if (data == null || offset < 0 || offset > data.length - 1 || offset + length > data.length) {
return 0;
}

crc = 0xFFFF;
for (i = 0; i < length; ++i) {
    crc ^= data[offset + i] << 8;
    for (j = 0; j < 8; ++j) {
        crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
    }
}
return crc & 0xFFFF;

}

function crc16(data, offset, length) {
if (data == null || offset < 0 || offset > data.length - 1 || offset + length > data.length) {
return 0;
}

crc = 0xFFFF;
for (i = 0; i < length; ++i) {
    crc ^= data[offset + i] << 8;
    for (j = 0; j < 8; ++j) {
        crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
    }
}
return crc & 0xFFFF;

}

I pasted the code there and it gave an error do you know how to make an extension?


Show us an example of input and output, for example
crc(123,0,2) return --> 15802

https://gist.github.com/tijnkooijmans/10981093

@SimpleFunction(description = "crc16")
public int crc16(String date, int offset, int length) {	
byte[] data = date.getBytes();
        if (data == null || offset < 0 || offset > data.length - 1 || offset + length > data.length) {
            return 0;
        }

        int crc = 0xFFFF;
        for (int i = 0; i < length; ++i) {
            crc ^= data[offset + i] << 8;
            for (int j = 0; j < 8; ++j) {
                crc = (crc & 0x8000) > 0 ? (crc << 1) ^ 0x1021 : crc << 1;
            }
        }
        return crc & 0xFFFF;
    }

I don't understand how the code works what I know is that I have to calculate the crc 16 cciitt false from a text I have, I don't understand how to provide the input data like your example there

What result did you expect?
What information did you enter?

just the text of the text box, what I want to calculate the crc (using his example I used the same blocks and put the text and nothing else)

I want to calculate the crc of this text (00020126360014BR.GOV.BCB.PIX0114+55649995929035204000053039865802BR5917Leide dayane lima6007Acreuna62070503***6304) and I know the result is (2418) in hexadecimal

1 Like

Try
crc16.aia (6.2 KB)
com.KIO4_crc16.aix (5.5 KB)

3 Likes

thank you very much, that's exactly what I wanted, thank you very much♥️

1 Like


So what did you learn from this??

1 Like

DO YOUR homework.... if not, You're going to fail.... must do BY YOURSELF, try first, then ask for help

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.