public
static
String
toHex
(
Long serialNum, int length
) {
// String.format("%016x", 1) 将1专程16进制,不足的以0补上
return
String
.
format
(
"%0"
+ length +
"x"
,
Long
.
valueOf
(serialNum));
关于String.format()的使用在下篇文章整理
2.将十六进制高低位转换
* 十六进制字符串高低位转换
* @param hex
* @return
* @author wyc
* @date 2022-11-25
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()
;
3.调用方法
* 得到十六进制的字符串,可指定需要转换的长度
*
@param
serialNum 串行num
*
@return
{
@link String
}
public
static
String
getHexNum
(
Long serialNum, int length
) {
return
reverse
(
hex
(serialNum, length));
* main测试方法
*
@param
args arg游戏
public
static
void
main
(
String
[] args
){
for
(long i =
0
; i <
20
; i++){
System
.
out
.
println
(
"十进制:"
+ i +
",转换后十六进制:"
+
getHexCardNum
(i,
16
));