我正在尝试将一些Javascript代码移植到Java。我已经成功移植了代码,但它无效 - 每次都在最后一个数字处失败。
检查验证的链接:http://imei-number.com/imei-validation-check/
这是我的Javascript代码:
function imei_gen() {
var pos;
var str = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var sum = 0;
var final_digit = 0;
var t = 0;
var len_offset = 0;
var len = 15;
var issuer;
var rbi = ["01", "10", "30", "33", "35", "44", "45", "49", "50", "51", "52", "53", "54", "86", "91", "98", "99"];
var arr = rbi[Math.floor(Math.random() * rbi.length)].split("");
str[0] = Number(arr[0]);
str[1] = Number(arr[1]);
pos = 2;
while (pos < len - 1) {
str[pos++] = Math.floor(Math.random() * 10);
}
len_offset = (len + 1) % 2;
for (pos = 0; pos < len - 1; pos++) {
if ((pos + len_offset) % 2) {
t = str[pos] * 2;
if (t > 9) {
t -= 9;
}
sum += t;
} else {
sum += str[pos];
}
}
final_digit = (10 - (sum));
str[len - 1] = final_digit;
t = str.join('');
t = t.substr(0, len);
return t;
}
print(imei_gen());
我使用过ScriptEngine,但是花了太多时间,差不多有2秒钟就完成了。
以下是实施:
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
public class SriptEngineJava {
public static void main(String args[]){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
try {
FileReader reader = new FileReader("imei.js");
engine.eval(reader);
//reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
最后是Java实现:
public class Imei {
public static void main(String[] args) {
Imei.imei_gen();
}
public static void imei_gen(){
int pos;
int str[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int sum = 0;
int final_digit = 0;
int t = 0;
int len_offset = 0;
int len = 15;
int issuer;
String rbi[] = {"01","10","30","33","35","44","45","49","50","51","52","53","54","86","91","98","99"};
String arr[] = rbi[(int) Math.floor(Math.random() * rbi.length)].split("");
str[0] = Integer.parseInt(arr[0]);
str[1] = Integer.parseInt(arr[1]);
pos = 2;
while (pos < len - 1) {
str[pos++] = (int) (Math.floor(Math.random() * 10) % 10);
}
len_offset = (len + 1) % 2;
for (pos = 0; pos < len - 1; pos++) {
if (((pos + len_offset) % 2)==0) {
t = str[pos] * 2;
if (t > 9) {
t -= 9;
}
sum += t;
}
else {
sum += str[pos];
}
}
final_digit = (10 - (sum % 10)) % 10;
str[len - 1] = final_digit;
String s = "";
for(int i=0;i<str.length;i++){
s += str[i];
}
System.out.println(s.substring(0, len));
}
}
使用ScriptEngine输出是正确的,但只使用Java,最后一位数字没有得到正确的值。
请检查此网址以验证,您将了解错误是什么: http://imei-number.com/imei-validation-check/
答案 0 :(得分:1)
您可能想要了解Java中Luhn算法的这种实现:
public class luhn {
/**
Calculate the Luhn sum of the input number
@param str number to check
@return sum calcuted with Luhn algorithm
**/
public static int luhnTest(String str){
int sum=0;
boolean isEven=false;
for (int i=str.length();i>0;i--){
int k=Integer.parseInt(str.substring(i-1,i));
if(isEven){
k=k*2;
if(k/10!=0)
k=k/10+k%10;
}
isEven=!isEven;
sum+=k;
}
return sum;
}
/**
Return true if the input number is valid according to the Luhn formula
@param str number to check
**/
public static boolean isLuhnValid(String str){
if (luhnTest(str)%10==0)
return true;
return false;
}
/**
Return the check digit that makes the input number valid according to the Luhn formula
@param str number to check
@return calculated check digit
**/
public static int getCheckDigit(String str){
int k=luhnTest(str+"0");
int i=0;
if(k%10!=0)
i=10-k%10;
return i;
}
/**
Return the input number with check digit appended
@param str number to make Luhn valid
@return Luhn valid number
**/
public static String getLuhnNumber(String str){
return str+getCheckDigit(str);
}
}