package me.zhengjie.utils;

import java.util.zip.CRC32;

public class CRC32Utils {

    public static String getUnsignedCRC32(String input) {
        CRC32 crc32 = new CRC32();
        crc32.update(input.getBytes());
        int value = (int) crc32.getValue();
        // 将int转换为long以避免潜在的符号扩展问题
        int unsignedValue = value & 0xFFFFFFFF;
        String hexString = formatCrcToHex(unsignedValue);
        return "0x" + hexString; // 0x8abd91cc
    }

    private static String formatCrcToHex(int crc) {
        // 方法一：采用String.format
        String format = String.format("%08x", crc);
        return format.toUpperCase();
    }

    public static void main(String[] args) {
        String text = "{\"Name\":\"4KL200URH300.4K\",\"MachineName\":\"3DPLUS\",\"MachineType\":\"Spade\",\"MainboardID\":\"12345678\",\"ProtocolVersion\":\"V3.0.0\",\"FirmwareVersion\":\"V1.0.0\"}";
        String crc32Value = getUnsignedCRC32(text);
        System.out.println("CRC-32 (unsigned): " + crc32Value);
    }
}
