计算LRC在Java

问题描述:

我想要计算LRC为以下消息:计算LRC在Java

 
T = 0101 0100 
P = 0101 0000 
1 = 0011 0001 
2 = 0011 0010 


Starting with 0x00 as the initial byte. 

0 XOR ‘T’:   0000 0000 
0101 0100 
Result, LRC = 0101 0100 


LRC XOR ‘P’: 0101 0100 
         0101 0000 
Result, LRC = 0000 0100 


LRC XOR ‘1’: 0000 0100 
         0011 0001 
Result, LRC = 0011 0101 


LRC XOR ‘2’: 0011 0101 
         0011 0010 
Result, LRC = 0000 0111

到目前为止我已经尝试的代码如下所示:

public class TexttoBinary { 

private static final int maxBytes = 1; 

public static int convert(char number) { 

    // BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
    System.out.print("Type the number to parse: "); 
    //int number = Integer.parseInt(in.readLine()); 
    int Bit; 
    //char number = '1'; 
    //UInt8 i; 
    String result = ""; 
    for (int i = maxBytes * 7; i >=0; i--) { 
     Bit = 1 << i; 
     if (number >= Bit) { 
      result += 1; 
      //System.out.println(result); 
      number -= Bit; 
     } else { 
      result += 0; 
     } 
    } 
    System.out.println(result); 
    return Integer.parseInt(result); 
} 
public static void main(String args[]){ 
    String msg ="TP12"; 
    char[] toCharArray = msg.toCharArray(); 
    char lrc=0; 
    for(int i=0;i<toCharArray.length;i++) 
    lrc ^=convert(toCharArray[i]); 
    System.out.println((byte)lrc); 
} 

}

输出I我越来越不同了。现在我该怎么做 ?

+2

什么是“LRC”? – dkamins 2011-06-03 00:39:06

+1

你现在要做的是调试你的代码。 – 2011-06-03 00:40:42

+0

@dkamins:LRC是纵向冗余检查 – Deepak 2011-06-03 00:43:12

假设你在谈论纵向冗余检查;当有疑问时,check you algorithm against published examples

Here's one in Java,这看起来与你的完全不同。也许你的尝试是利用德摩根的规范来优化LRC,但是很可能它会带来一个错误。

+0

但这并没有给我8位LRC。我怎么得到8位LRC从达? – Deepak 2011-06-03 00:55:50

+0

我越来越97 7 – Deepak 2011-06-03 00:56:55

+0

@Deepak,一个字节被定义为8位,异或操作位清,所以对两个字节进行异或运算与循环和异或一个字节完全相同 – 2011-06-03 03:15:23

以下代码在java中为我工作,用于验证信用卡磁条读取。 see it running online here.

String str = ";4564456445644564=1012101123456789?"; 
byte lrc = 0x00; 
byte [] binaryValue = str.getBytes(); 

for(byte b : binaryValue) { 

    lrc ^= b; 
} 

// lrc msut be between 48 to 95 
lrc %= 48; 
lrc += 48; 

System.out.println("LRC: " + (char) lrc);