- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在 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
非常感谢您的帮助。
最佳答案
您的规范说明:
这是一个返回预期结果的代码片段,但我不知道帧号来自哪里(当然在规范的其他地方)
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));
}
}
关于java - 如何计算 "binary values addition of characters"的校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364999/
如何在 PHP 中生成 CRC-8 校验和? 最佳答案 function crcnifull ($dato, $byte) { static $PolyFull=0x8c; for ($i=0
我正在编写代码来使用 32 位无符号整数计算 CRC16。当尝试打印执行 CRC 操作的 XOR 函数的返回值时,它总是打印 0。我尝试了各种调试方法,例如打印语句,但是,我似乎无法弄清楚! 这是我的
ThinkPHP3.2.3验证码显示、刷新、校验 ,具体如下: 显示验证码 首先在Home/Controller下创建一个公共控制器PublicController
我想将自定义验证绑定(bind)到 TimePicker 自定义控件,但下面的代码显示“无法将内容添加到 TimePicker 的对象类型。”。
目录 Spring 校验(validator,JSR-303)实现 什么是JSR-303规范 与Spring MVC结合 实体类添加
导包和配置 导入 JSR 303 的包、hibernate valid 的包 ?
我是一名优秀的程序员,十分优秀!