Untitled

 avatar
unknown
plain_text
6 days ago
1.3 kB
5
Indexable
public class CRC8WCDMA {

    private static final int POLYNOMIAL = 0x9B; // Wielomian dla CRC-8-WCDMA
    private static final int INITIAL_VALUE = 0x00; // Początkowa wartość dla CRC-8-WCDMA

    public static void main(String[] args) {
        String nodeId = "39223d25-e021-49cd-b490-ef111ca11d13"; // Z myślnikami
        int crcWithHyphens = calculateCRC8WCDMA(nodeId);
        System.out.println("CRC-8-WCDMA (z myślnikami): " + crcWithHyphens); // Powinno zwrócić 84

        String nodeIdWithoutHyphens = nodeId.replace("-", ""); // Bez myślników
        int crcWithoutHyphens = calculateCRC8WCDMA(nodeIdWithoutHyphens);
        System.out.println("CRC-8-WCDMA (bez myślników): " + crcWithoutHyphens); // Powinno zwrócić inną wartość
    }

    public static int calculateCRC8WCDMA(String data) {
        int crc = INITIAL_VALUE;

        for (char c : data.toCharArray()) {
            crc ^= (c & 0xFF); // XOR z każdym bajtem danych
            for (int i = 0; i < 8; i++) {
                if ((crc & 0x80) != 0) {
                    crc = (crc << 1) ^ POLYNOMIAL;
                } else {
                    crc <<= 1;
                }
                crc &= 0xFF; // Upewnij się, że CRC pozostaje w zakresie 8-bitów
            }
        }

        return crc;
    }
}
Leave a Comment