| | |
| | | val data = ByteArray(16) |
| | | try { |
| | | // 设置国家行政区域号(BCD格式,6字节,0-5位) |
| | | val areaCodeBytes = BcdUtil.strToBcd(String.format("%012d", areaNumber)) |
| | | System.arraycopy(areaCodeBytes, 0, data, 0, 6) |
| | | val areaNumberStr = if (areaNumber.isBlank() || !areaNumber.all { it.isDigit() }) { |
| | | "000000000000" |
| | | } else { |
| | | areaNumber.padStart(12, '0') |
| | | } |
| | | val areaCodeBytes = BcdUtil.strToBcd(areaNumberStr) |
| | | System.arraycopy(areaCodeBytes, 0, data, 0, minOf(areaCodeBytes.size, 6)) |
| | | |
| | | // 设置用户卡编号(HEX格式,2字节,6-7位) |
| | | // 设置用户卡编号(HEX格式,2字节,6-7位) - 修复:确保数组长度正确 |
| | | val userCodeBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(userCodeNumber)) |
| | | System.arraycopy(userCodeBytes, 0, data, 6, 2) |
| | | val userCodePadded = ByteArray(2) |
| | | System.arraycopy(userCodeBytes, 0, userCodePadded, 0, minOf(userCodeBytes.size, 2)) |
| | | System.arraycopy(userCodePadded, 0, data, 6, 2) |
| | | |
| | | // 设置卡类型(8位) |
| | | data[8] = HexUtil.hexToByte(cardType) |
| | | |
| | | // 设置手机号(BCD格式,6字节,9-14位) |
| | | val phoneBytes = BcdUtil.strToBcd(phoneNumber.padStart(12, '0')) |
| | | System.arraycopy(phoneBytes, 0, data, 9, 6) |
| | | System.arraycopy(phoneBytes, 0, data, 9, minOf(phoneBytes.size, 6)) |
| | | |
| | | // 设置校验和(15位) |
| | | data[15] = getByteSum(data) |
| | |
| | | try { |
| | | data[0] = projectCode.toByte() |
| | | |
| | | // 设置余额 |
| | | val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)) |
| | | System.arraycopy(balanceBytes, 0, data, 1, 4) |
| | | // 设置余额 - 修复:确保数组长度正确 |
| | | val balanceHex = HexUtil.get10To16LowHigh(balance) |
| | | val balanceBytes = HexUtil.hexToByteArray(balanceHex) |
| | | val balancePadded = ByteArray(4) |
| | | System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4)) |
| | | System.arraycopy(balancePadded, 0, data, 1, 4) |
| | | |
| | | // 设置剩余水量 |
| | | val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater)) |
| | | System.arraycopy(waterBytes, 0, data, 5, 4) |
| | | // 设置剩余水量 - 修复:确保数组长度正确 |
| | | val waterHex = HexUtil.get10To16LowHigh(surplusWater) |
| | | val waterBytes = HexUtil.hexToByteArray(waterHex) |
| | | val waterPadded = ByteArray(4) |
| | | System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4)) |
| | | System.arraycopy(waterPadded, 0, data, 5, 4) |
| | | |
| | | // 设置电价 |
| | | // 设置电价 - 修复:确保数组长度正确 |
| | | val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(electricPrice)) |
| | | System.arraycopy(priceBytes, 0, data, 9, 2) |
| | | val pricePadded = ByteArray(2) |
| | | System.arraycopy(priceBytes, 0, pricePadded, 0, minOf(priceBytes.size, 2)) |
| | | System.arraycopy(pricePadded, 0, data, 9, 2) |
| | | |
| | | // 设置充值时间 |
| | | rechargeDate?.let { |
| | |
| | | fun toBytes(): ByteArray { |
| | | val data = ByteArray(16) |
| | | try { |
| | | // 备份余额和水量数据 |
| | | val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)) |
| | | System.arraycopy(balanceBytes, 0, data, 1, 4) |
| | | // 备份余额和水量数据 - 修复:确保数组长度正确 |
| | | val balanceHex = HexUtil.get10To16LowHigh(balance) |
| | | val balanceBytes = HexUtil.hexToByteArray(balanceHex) |
| | | val balancePadded = ByteArray(4) |
| | | System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4)) |
| | | System.arraycopy(balancePadded, 0, data, 1, 4) |
| | | |
| | | val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater)) |
| | | System.arraycopy(waterBytes, 0, data, 5, 4) |
| | | val waterHex = HexUtil.get10To16LowHigh(surplusWater) |
| | | val waterBytes = HexUtil.hexToByteArray(waterHex) |
| | | val waterPadded = ByteArray(4) |
| | | System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4)) |
| | | System.arraycopy(waterPadded, 0, data, 5, 4) |
| | | |
| | | // 设置水价 |
| | | // 设置水价 - 修复:确保数组长度正确 |
| | | val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(waterPrice)) |
| | | System.arraycopy(priceBytes, 0, data, 9, 2) |
| | | val pricePadded = ByteArray(2) |
| | | System.arraycopy(priceBytes, 0, pricePadded, 0, minOf(priceBytes.size, 2)) |
| | | System.arraycopy(pricePadded, 0, data, 9, 2) |
| | | |
| | | // 设置充值时间 |
| | | rechargeDate?.let { |