关于java与C的一些思考

1 篇文章 0 订阅
1 篇文章 0 订阅

  最近因为课业需要学习了CRC校验方面的一些知识,经过学习别人的笔记,结合自己的一些思考,对java和c有了新的认识。下面以查表CRC8算法为例:

  c版本:

  

 
 
#include <stdio.h>

const unsigned char crc8_tab [] = { 0 , 94 , 188 , 226 , 97 , 63 , 221 , 131 , 194 , 156 , 126 , 32 , 163 , 253 , 31 , 65 , 157 , 195 , 33 , 127 , 252 , 162 , 64 , 30 , 95 , 1 , 227 , 189 , 62 , 96 , 130 , 220 , 35 , 125 , 159 , 193 , 66 , 28 , 254 , 160 , 225 , 191 , 93 , 3 , 128 , 222 , 60 , 98 , 190 , 224 , 2 , 92 , 223 , 129 , 99 , 61 , 124 , 34 , 192 , 158 , 29 , 67 , 161 , 255 , 70 , 24 , 250 , 164 , 39 , 121 , 155 , 197 , 132 , 218 , 56 , 102 , 229 , 187 , 89 , 7 , 219 , 133 , 103 , 57 , 186 , 228 , 6 , 88 , 25 , 71 , 165 , 251 , 120 , 38 , 196 , 154 , 101 , 59 , 217 , 135 , 4 , 90 , 184 , 230 , 167 , 249 , 27 , 69 , 198 , 152 , 122 , 36 , 248 , 166 , 68 , 26 , 153 , 199 , 37 , 123 , 58 , 100 , 134 , 216 , 91 , 5 , 231 , 185 , 140 , 210 , 48 , 110 , 237 , 179 , 81 , 15 , 78 , 16 , 242 , 172 , 47 , 113 , 147 , 205 , 17 , 79 , 173 , 243 , 112 , 46 , 204 , 146 , 211 , 141 , 111 , 49 , 178 , 236 , 14 , 80 , 175 , 241 , 19 , 77 , 206 , 144 , 114 , 44 ,
109 , 51 , 209 , 143 , 12 , 82 , 176 , 238 , 50 , 108 , 142 , 208 , 83 , 13 , 239 , 177 , 240 , 174 , 76 , 18 , 145 , 207 , 45 , 115 , 202 , 148 , 118 , 40 , 171 , 245 , 23 , 73 , 8 , 86 , 180 , 234 , 105 , 55 , 213 , 139 , 87 , 9 , 235 , 181 , 54 , 104 , 138 , 212 , 149 , 203 , 41 , 119 , 244 , 170 , 72 , 22 , 233 , 183 , 85 , 11 , 136 , 214 , 52 , 106 , 43 , 117 , 151 , 201 , 74 , 20 , 246 , 168 , 116 , 42 , 200 , 150 , 21 , 75 , 169 , 247 , 182 , 232 , 10 , 84 , 215 , 137 , 107 , 53 };

unsigned char calc_crc8 ( const unsigned char * data , int len ) {
unsigned char ret = 0 ;
for (; len > 0 ; len -- ) {
ret = crc8_tab [ ret ^ * data ];
data ++ ;
}
return ( ret );
}

int main ( int argc , const char * argv []){
unsigned char d [] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
printf ( "crc8=0x%02x \n " , calc_crc8 ( d , sizeof ( d )));
return 0 ;
}
java版本:
  1. public class CRC8 {  
  2.     static byte[] crc8_tab = { (byte0, (byte94, (byte188, (byte226, (byte97, (byte63, (byte221, (byte131, (byte194, (byte156, (byte126, (byte32, (byte163, (byte253, (byte31, (byte65, (byte157, (byte195, (byte33, (byte127, (byte252, (byte162, (byte64, (byte30, (byte95, (byte1, (byte227, (byte189, (byte62, (byte96, (byte130, (byte220, (byte35, (byte125, (byte159, (byte193, (byte66, (byte28, (byte254, (byte160, (byte225, (byte191, (byte93, (byte3, (byte128, (byte222, (byte60, (byte98, (byte190, (byte224, (byte2, (byte92, (byte223, (byte129, (byte99, (byte61, (byte124, (byte34, (byte192, (byte158, (byte29, (byte67, (byte161, (byte255, (byte70, (byte24,  
  3.             (byte250, (byte164, (byte39, (byte121, (byte155, (byte197, (byte132, (byte218, (byte56, (byte102, (byte229, (byte187, (byte89, (byte7, (byte219, (byte133, (byte103, (byte57, (byte186, (byte228, (byte6, (byte88, (byte25, (byte71, (byte165, (byte251, (byte120, (byte38, (byte196, (byte154, (byte101, (byte59, (byte217, (byte135, (byte4, (byte90, (byte184, (byte230, (byte167, (byte249, (byte27, (byte69, (byte198, (byte152, (byte122, (byte36, (byte248, (byte166, (byte68, (byte26, (byte153, (byte199, (byte37, (byte123, (byte58, (byte100, (byte134, (byte216, (byte91, (byte5, (byte231, (byte185, (byte140, (byte210, (byte48, (byte110, (byte237,  
  4.             (byte179, (byte81, (byte15, (byte78, (byte16, (byte242, (byte172, (byte47, (byte113, (byte147, (byte205, (byte17, (byte79, (byte173, (byte243, (byte112, (byte46, (byte204, (byte146, (byte211, (byte141, (byte111, (byte49, (byte178, (byte236, (byte14, (byte80, (byte175, (byte241, (byte19, (byte77, (byte206, (byte144, (byte114, (byte44, (byte109, (byte51, (byte209, (byte143, (byte12, (byte82, (byte176, (byte238, (byte50, (byte108, (byte142, (byte208, (byte83, (byte13, (byte239, (byte177, (byte240, (byte174, (byte76, (byte18, (byte145, (byte207, (byte45, (byte115, (byte202, (byte148, (byte118, (byte40, (byte171, (byte245, (byte23, (byte73, (byte8,  
  5.             (byte86, (byte180, (byte234, (byte105, (byte55, (byte213, (byte139, (byte87, (byte9, (byte235, (byte181, (byte54, (byte104, (byte138, (byte212, (byte149, (byte203, (byte41, (byte119, (byte244, (byte170, (byte72, (byte22, (byte233, (byte183, (byte85, (byte11, (byte136, (byte214, (byte52, (byte106, (byte43, (byte117, (byte151, (byte201, (byte74, (byte20, (byte246, (byte168, (byte116, (byte42, (byte200, (byte150, (byte21, (byte75, (byte169, (byte247, (byte182, (byte232, (byte10, (byte84, (byte215, (byte137, (byte10753 };  
  6.   
  7.     /** 
  8.      * 计算数组的CRC8校验值 
  9.      *  
  10.      * @param data 
  11.      *            需要计算的数组 
  12.      * @return CRC8校验值 
  13.      */  
  14.     public static byte calcCrc8(byte[] data) {  
  15.         return calcCrc8(data, 0, data.length, (byte0);  
  16.     }  
  17.   
  18.     /** 
  19.      * 计算CRC8校验值 
  20.      *  
  21.      * @param data 
  22.      *            数据 
  23.      * @param offset 
  24.      *            起始位置 
  25.      * @param len 
  26.      *            长度 
  27.      * @return 校验值 
  28.      */  
  29.     public static byte calcCrc8(byte[] data, int offset, int len) {  
  30.         return calcCrc8(data, offset, len, (byte0);  
  31.     }  
  32.   
  33.     /** 
  34.      * 计算CRC8校验值 
  35.      *  
  36.      * @param data 
  37.      *            数据 
  38.      * @param offset 
  39.      *            起始位置 
  40.      * @param len 
  41.      *            长度 
  42.      * @param preval 
  43.      *            之前的校验值 
  44.      * @return 校验值 
  45.      */  
  46.     public static byte calcCrc8(byte[] data, int offset, int len, byte preval) {  
  47.         byte ret = preval;  
  48.         for (int i = offset; i < (offset + len); ++i) {  
  49.             ret = crc8_tab[(0x00ff & (ret ^ data[i]))];  
  50.         }  
  51.         return ret;  
  52.     }  
  53.   
  54.     // 测试  
  55.     public static void main(String[] args) {  
  56.         byte crc = CRC8.calcCrc8(new byte[] { 12345678910 });  
  57.         System.out.println("" + Integer.toHexString(0x00ff & crc));  
  58.     }  
  59. }
可以看到二者大同小异。而唯一的区别就是c中操作数为指针,而java中操作数为byte[]数组。因为java为高级语言所以取消了可以对内存的直接操作的指针,
但是这就造成了java对于二进制数按位操作的不方便。两种算法看起来差不多但运行结果并不相同,原因是CRC校验是通过原数据逐个移位与生成表达式异或得到
最后的校验和,因此需要对data地址++按位偏移。而java按位操作通常通过byte[]数组,而此处byte数组的一次偏移实际上是原数据偏移了8位。因此求出的
校验和肯定是错误的。
目前没有想到可以用java按位依次操作的方式,还需继续学习!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值