Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算;二进制值字符加法“;校验和_Java - Fatal编程技术网

Java 如何计算;二进制值字符加法“;校验和

Java 如何计算;二进制值字符加法“;校验和,java,Java,在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 ad

在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.

非常感谢您的帮助。

您的说明书上说:

  • 校验和应使用帧编号初始化
下面是一个返回预期结果的代码段,但我不知道帧编号来自何处(当然,在您的规范的其他地方)

公共类ChecksumBuilder{
公共静态字符串getFrameCheckSum(字符串frametext,int framenum)
{
字节[]a=frametext.getBytes();
int校验和=framenum;

对于(int i=0;i,您的规范说明:

  • 校验和应使用帧编号初始化
下面是一个返回预期结果的代码段,但我不知道帧编号来自何处(当然,在您的规范的其他地方)

公共类ChecksumBuilder{
公共静态字符串getFrameCheckSum(字符串frametext,int framenum)
{
字节[]a=frametext.getBytes();
int校验和=framenum;

对于(int i=0;我非常感谢您的帮助!无论如何,我刚刚意识到规范的格式不正确,因为没有将其放入代码块中。我修改了帖子,所以现在可以了。您将看到含义可能不是您第一次读它时理解的含义…抱歉:我的错!帧号是第一个“3”我的消息中的字符,只是告诉您校验和计算必须从那里开始。再次感谢您的帮助。是的,谢谢您的帮助,但它并没有真正回答问题。您的代码与我的代码非常相似,并且不遵守规范。但好的,我为您的好意投了一票;)非常感谢您的帮助!无论如何,我刚刚意识到规范的格式不正确,因为没有将其放入代码块中。我修改了帖子,所以现在可以了。您将看到,您在第一次阅读时可能不理解它的含义…抱歉:我的错!帧号是第一个“3”我的消息中的字符,只是告诉您校验和计算必须从那里开始。再次感谢是的,谢谢您的帮助,但它并没有真正回答问题。您的代码与我的代码非常相似,并且不遵守规范。但是好的,我投您一票,感谢您的好意;)“它似乎不起作用”-你的确切意思是什么?“它似乎不起作用”-你的确切意思是什么?
    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
public class ChecksumBuilder {
public static String getFrameCheckSum(String frametext,int framenum)
{
    byte[] a=frametext.getBytes();

    int checksum=framenum;
    for(int i=0;i<a.length;i++)
    {           
        checksum+=a[i];
    }

    String out=String.format("%02x",(checksum & 0xFF)).toUpperCase();
    return out;
}

public static void main(String[] args)
{
    System.out.print(ChecksumBuilder.getFrameCheckSum("3L|1<CR>",1));
}