/**
* @program: simple_tools
* @description: 用户卡生成规则类 卡号原则:将10进制卡号转成16进制卡号
* @author: Mr.chen
* @create: 2020-05-19 10:54
**/
public class UserCardUtil {
/**
* 逆序V卡卡号变正序(仅支持偶数位反转)
*
* @param cardId
* @return
*/
public static String reverseCardId(String cardId) {
String[] arrs = new String[cardId.length()];
for (int i = 0; i < cardId.length(); i++) {
if (i == (cardId.length() - 1)) {
arrs[i] = cardId.substring(i);
} else {
arrs[i] = cardId.substring(i, i + 1);
}
}
for (int i = 0; i < arrs.length; i++) {
if ((i % 2) == 1) {
String temp = arrs[i];
arrs[i] = arrs[i - 1];
arrs[i - 1] = temp;
}
}
StringBuffer newCardId = new StringBuffer();
for (int i = arrs.length - 1; i >= 0; i--) {
newCardId.append(arrs[i]);
}
return newCardId.toString();
}
/**
* 十进制卡转16进制卡
*
* @param cardId
* @return
*/
public static String decimal2Hex(String cardId) {
String hexCardId = Long.toHexString(Long.parseLong(cardId));
if (hexCardId.length() < 8) {
int spaceZeroCount = 8 - hexCardId.length();
for (int i = 0; i < spaceZeroCount; i++) {
hexCardId = "0" + hexCardId;
}
}else if(hexCardId.length()%2>0){
hexCardId="0"+hexCardId;
}
return hexCardId.toUpperCase();
}
/**
* 16进制卡转十进制卡
* @param hex
* @return
*/
/*
* 16进制字符串转字符串
*/
public static String hex2String(String hex){
if(hex == null || hex.length()==0){
return hex;
}
return String.valueOf(Long.parseLong(hex, 16));
}
public static void main(String[] args) {
System.out.println(UserCardUtil.hex2String(UserCardUtil.reverseCardId("00126F5E6903")));
System.out.println(UserCardUtil.decimal2Hex("3751090786816"));
System.out.println(UserCardUtil.decimal2Hex("3751090786816"));
}
}