Code39中的校验和(Check Sum)实现

 

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));
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值