Untitled
unknown
plain_text
3 months ago
1.2 kB
12
Indexable
private static String calculateBodyLength(String message) { // Find the position of the BodyLength placeholder int bodyLengthIndex = message.indexOf("9=#"); if (bodyLengthIndex == -1) { throw new RuntimeException("BodyLength placeholder not found"); } // Find the end of the BodyLength field (after the SOH) int bodyStart = message.indexOf(SOH, bodyLengthIndex) + 1; if (bodyStart == 0) { throw new RuntimeException("Invalid message format"); } // Calculate length from bodyStart to end of message (before checksum) String body = message.substring(bodyStart); int length = body.getBytes(StandardCharsets.US_ASCII).length; return message.replaceFirst("9=#", "9=" + length); } private static String addCheckSum(String message) { // Calculate checksum for entire message except the checksum field int checksum = 0; byte[] messageBytes = message.getBytes(StandardCharsets.US_ASCII); for (byte b : messageBytes) { checksum += b & 0xFF; } checksum %= 256; // Append checksum field return message + "10=" + String.format("%03d", checksum) + SOH; }
Editor is loading...
Leave a Comment