/**
* @program: chargingpile
* @description: 转成16进制卡号的工具类
* @author: Mr.ch
* @create: 2020-08-31
**/
public class HexUtils {

/**
* 解密后的卡号需要高低位转换(不足8位前面补0)
* 才能得到需要的16进制卡号
* @param serialNum
* @return
*/
public static String getHexCardNum(String serialNum) {
return reverse(hex(Integer.parseInt(serialNum)));
}

/**
* 10进制转16进制
* @param serialNum
* @return
*/
public static String hex(int serialNum) {
return String.format("%08x", Integer.valueOf(serialNum));
}

/**
* 十六进制字符串高低位转换
* @param hex
* @return
*/
public static String reverse( String hex) {
final char[] charArray = hex.toCharArray();
final int length = charArray.length;
final int times = length / 2;
for (int c1i = 0; c1i < times; c1i += 2) {
final int c2i = c1i + 1;
final char c1 = charArray[c1i];
final char c2 = charArray[c2i];
final int c3i = length - c1i - 2;
final int c4i = length - c1i - 1;
charArray[c1i] = charArray[c3i];
charArray[c2i] = charArray[c4i];
charArray[c3i] = c1;
charArray[c4i] = c2;
}
return new String(charArray).toUpperCase();
}
}

使用方法

 @Test
    public void testGetStr() {
        //1000 转16进制为 3e8 
        // 补位为000003e8
        //高低位转换 E8030000
         System.out.println(getHexCardNum("1000"));
    }

 

————————————————
版权声明:本文为CSDN博主「污青」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44851436/article/details/108348195

posted on 2021-04-01 15:28  丁昆  阅读(900)  评论(0编辑  收藏  举报