我想要計算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我越來越不同了。現在我該怎麼做 ?
什麼是「LRC」? – dkamins 2011-06-03 00:39:06
你現在要做的是調試你的代碼。 – 2011-06-03 00:40:42
@dkamins:LRC是縱向冗餘檢查 – Deepak 2011-06-03 00:43:12