Code 39でのチェックサム(Check Sum)実装
2261 ワード
詳細
private int getCharIndex(char c) throws InvalidAtributeException{
if( c >= '0' && c <= '9' ){
return c - 48;
} else if( c >= 'A' && c <= 'Z' ){
return c - 55;
} else if( c == '-' ){
return 36;
} else if( c == '.' ){
return 37;
} else if( c == ' ' ){
return 38;
} else if( c == '$' ){
return 39;
} else if( c == '/' ){
return 40;
} else if( c == '+' ){
return 41;
} else if( c == '%' ){
return 42;
} else {
throw new InvalidAtributeException("[Code39] The text contains unsuported chars.");
}
}
private int getChar(int c) throws InvalidAtributeException{
if( c >= 0 && c <= 9 ){
return c + 48;
} else if( c >= 10 && c <= 35){
return c + 55;
} else if( c == 36 ){
return 45;
} else if( c == 37 ){
return 46;
} else if( c == 38 ){
return 32;
} else if( c == 39 ){
return 36;
} else if( c == 40 ){
return 47;
} else if( c == 41 ){
return 43;
} else if( c == 42 ){
return 37;
} else {
throw new InvalidAtributeException("[Code39] The text contains unsuported chars.");
}
}
public String computeCheckSum(String texto) throws InvalidAtributeException {
int check = 0;
for( int i = 0; i < texto.length(); i++ ){
check += getCharIndex(texto.charAt(i));
}
return (Character.valueOf((char)getChar(check%43)).toString());
}
@Test
public void testComputeCheckSum() throws InvalidAtributeException {
String code = "TEXT";
System.out.println(computeCheckSum(code));
}