Работа в Java, здесь спецификация я есть для того, чтобы реализовать вычисление контрольной суммы на символьных сообщений:Как вычислить «двоичные значения сложение символов» для контрольной суммы
8.3.3 Checksum—The checksum permits the receiver to detect a defective frame. The checksum is encoded as two characters which are sent after the <ETB> or <ETX> character. The checksum is computed by adding the binary values of the characters, keeping the least significant eight bits of the result.
8.3.3.1 The checksum is initialized to zero with the <STX> character. The first character used in computing the checksum is the frame number. Each character in the message text is added to the checksum (modulo 256). The computation for the checksum does not include <STX>, the checksum characters, or the trailing <CR> and <LF>.
8.3.3.2 The checksum is an integer represented by eight bits, it can be considered as two groups of four bits. The groups of four bits are converted to the ASCII characters of the hexadecimal representation. The two ASCII characters are transmitted as the checksum, with the most significant character first.
8.3.3.3 For example, a checksum of 122 can be represented as 01111010 in binary or 7A in hexadecimal. The checksum is transmitted as the ASCII character 7 followed by the character A.
Вот что я понимаю и реализовано, но это, кажется, не работают ...:
private void computeAndAddChecksum(byte[] bytes, OutputStream outputStream) {
logBytesAsBinary(bytes);
long checksum = 0;
for (int i = 0; i < bytes.length; i++) {
checksum += (bytes[i] & 0xffffffffL);
}
int integerChecksum = (int)checksum;
String hexChecksum = Integer.toHexString(integerChecksum).toUpperCase();
logger.info("Checksum for "+new String(bytes)+" is "+checksum+" in hexa: "+hexChecksum);
try {
if (outputStream != null)
{
outputStream.write(hexChecksum.getBytes());
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}
ли вам какие-либо идеи, почему этот фрагмент не приспособленные к спецификации? Вот пример я был дан, если это может помочь:
<STX>3L|1<CR><ETX>3C<CR><LF>
так контрольная сумма
3L|1<CR><ETX>
должен быть
3C
Большое спасибо вам помочь.
«это не кажется, работают », - что * точно * вы имеете в виду? – Bohemian