001package top.cenze.utils; 002 003import cn.hutool.core.convert.Convert; 004import cn.hutool.core.util.ObjectUtil; 005import cn.hutool.core.util.StrUtil; 006 007import java.math.BigInteger; 008 009public class ConvertUtil { 010 /** 011 * 整形转换成网络传输的字节流(字节数组)型数据 012 * 013 * @param num 一个整型数据 014 * @return 4个字节的自己数组 015 */ 016 public static byte[] intToBytes(int num) { 017 byte[] bytes = new byte[4]; 018 bytes[0] = (byte) (0xff & (num >> 0)); 019 bytes[1] = (byte) (0xff & (num >> 8)); 020 bytes[2] = (byte) (0xff & (num >> 16)); 021 bytes[3] = (byte) (0xff & (num >> 24)); 022 return bytes; 023 } 024 025 /** 026 * 四个字节的字节数据转换成一个整形数据 027 * 028 * @param bytes 4个字节的字节数组 029 * @return 一个整型数据 030 */ 031 public static int byteToInt(byte[] bytes) { 032 int num = 0; 033 int temp; 034 temp = (0x000000ff & (bytes[0])) << 0; 035 num = num | temp; 036 temp = (0x000000ff & (bytes[1])) << 8; 037 num = num | temp; 038 temp = (0x000000ff & (bytes[2])) << 16; 039 num = num | temp; 040 temp = (0x000000ff & (bytes[3])) << 24; 041 num = num | temp; 042 return num; 043 } 044 045 /** 046 * 长整形转换成网络传输的字节流(字节数组)型数据 047 * 048 * @param num 一个长整型数据 049 * @return 4个字节的自己数组 050 */ 051 public static byte[] longToBytes(long num) { 052 byte[] bytes = new byte[8]; 053 for (int i = 0; i < 8; i++) { 054 bytes[i] = (byte) (0xff & (num >> (i * 8))); 055 } 056 057 return bytes; 058 } 059 060 /** 061 * 大数字转换字节流(字节数组)型数据 062 * 063 * @param n 064 * @return 065 */ 066 public static byte[] byteConvert32Bytes(BigInteger n) { 067 byte tmpd[] = (byte[]) null; 068 if (n == null) { 069 return null; 070 } 071 072 if (n.toByteArray().length == 33) { 073 tmpd = new byte[32]; 074 System.arraycopy(n.toByteArray(), 1, tmpd, 0, 32); 075 } else if (n.toByteArray().length == 32) { 076 tmpd = n.toByteArray(); 077 } else { 078 tmpd = new byte[32]; 079 for (int i = 0; i < 32 - n.toByteArray().length; i++) { 080 tmpd[i] = 0; 081 } 082 System.arraycopy(n.toByteArray(), 0, tmpd, 32 - n.toByteArray().length, n.toByteArray().length); 083 } 084 return tmpd; 085 } 086 087 /** 088 * 换字节流(字节数组)型数据转大数字 089 * 090 * @param b 091 * @return 092 */ 093 public static BigInteger byteConvertInteger(byte[] b) { 094 if (b[0] < 0) { 095 byte[] temp = new byte[b.length + 1]; 096 temp[0] = 0; 097 System.arraycopy(b, 0, temp, 1, b.length); 098 return new BigInteger(temp); 099 } 100 return new BigInteger(b); 101 } 102 103 /** 104 * 根据字节数组获得值(十六进制数字) 105 * 106 * @param bytes 107 * @return 108 */ 109 public static String getHexString(byte[] bytes) { 110 return getHexString(bytes, true); 111 } 112 113 /** 114 * 根据字节数组获得值(十六进制数字) 115 * 116 * @param bytes 117 * @param upperCase 118 * @return 119 */ 120 public static String getHexString(byte[] bytes, boolean upperCase) { 121 String ret = ""; 122 for (int i = 0; i < bytes.length; i++) { 123 ret += Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1); 124 } 125 return upperCase ? ret.toUpperCase() : ret; 126 } 127 128 /** 129 * 打印十六进制字符串 130 * 131 * @param bytes 132 */ 133 public static void printHexString(byte[] bytes) { 134 for (int i = 0; i < bytes.length; i++) { 135 String hex = Integer.toHexString(bytes[i] & 0xFF); 136 if (hex.length() == 1) { 137 hex = '0' + hex; 138 } 139 System.out.print("0x" + hex.toUpperCase() + ","); 140 } 141 System.out.println(""); 142 } 143 144 /** 145 * Convert hex string to byte[] 146 * 147 * @param hexString the hex string 148 * @return byte[] 149 */ 150 public static byte[] hexStringToBytes(String hexString) { 151 if (hexString == null || hexString.equals("")) { 152 return null; 153 } 154 155 hexString = hexString.toUpperCase(); 156 int length = hexString.length() / 2; 157 char[] hexChars = hexString.toCharArray(); 158 byte[] d = new byte[length]; 159 for (int i = 0; i < length; i++) { 160 int pos = i * 2; 161 d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); 162 } 163 return d; 164 } 165 166 /** 167 * Convert char to byte 168 * 169 * @param c char 170 * @return byte 171 */ 172 public static byte charToByte(char c) { 173 return (byte) "0123456789ABCDEF".indexOf(c); 174 } 175 176 /** 177 * 用于建立十六进制字符的输出的小写字符数组 178 */ 179 private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', 180 '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 181 182 /** 183 * 用于建立十六进制字符的输出的大写字符数组 184 */ 185 private static final char[] DIGITS_UPPER = {'0', '1', '2', '3', '4', '5', 186 '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 187 188 /** 189 * 将字节数组转换为十六进制字符数组 190 * 191 * @param data byte[] 192 * @return 十六进制char[] 193 */ 194 public static char[] encodeHex(byte[] data) { 195 return encodeHex(data, true); 196 } 197 198 /** 199 * 将字节数组转换为十六进制字符数组 200 * 201 * @param data byte[] 202 * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式 203 * @return 十六进制char[] 204 */ 205 public static char[] encodeHex(byte[] data, boolean toLowerCase) { 206 return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); 207 } 208 209 /** 210 * 将字节数组转换为十六进制字符数组 211 * 212 * @param data byte[] 213 * @param toDigits 用于控制输出的char[] 214 * @return 十六进制char[] 215 */ 216 protected static char[] encodeHex(byte[] data, char[] toDigits) { 217 int l = data.length; 218 char[] out = new char[l << 1]; 219 // two characters form the hex value. 220 for (int i = 0, j = 0; i < l; i++) { 221 out[j++] = toDigits[(0xF0 & data[i]) >>> 4]; 222 out[j++] = toDigits[0x0F & data[i]]; 223 } 224 return out; 225 } 226 227 /** 228 * 将字节数组转换为十六进制字符串 229 * 230 * @param data byte[] 231 * @return 十六进制String 232 */ 233 public static String encodeHexString(byte[] data) { 234 return encodeHexString(data, true); 235 } 236 237 /** 238 * 将字节数组转换为十六进制字符串 239 * 240 * @param data byte[] 241 * @param toLowerCase <code>true</code> 传换成小写格式 , <code>false</code> 传换成大写格式 242 * @return 十六进制String 243 */ 244 public static String encodeHexString(byte[] data, boolean toLowerCase) { 245 return encodeHexString(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER); 246 } 247 248 /** 249 * 将字节数组转换为十六进制字符串 250 * 251 * @param data byte[] 252 * @param toDigits 用于控制输出的char[] 253 * @return 十六进制String 254 */ 255 protected static String encodeHexString(byte[] data, char[] toDigits) { 256 return new String(encodeHex(data, toDigits)); 257 } 258 259 /** 260 * 将十六进制字符数组转换为字节数组 261 * 262 * @param data 十六进制char[] 263 * @return byte[] 264 * @throws RuntimeException 如果源十六进制字符数组是一个奇怪的长度,将抛出运行时异常 265 */ 266 public static byte[] decodeHex(char[] data) { 267 int len = data.length; 268 269 if ((len & 0x01) != 0) { 270 throw new RuntimeException("Odd number of characters."); 271 } 272 273 byte[] out = new byte[len >> 1]; 274 275 // two characters form the hex value. 276 for (int i = 0, j = 0; j < len; i++) { 277 int f = toDigit(data[j], j) << 4; 278 j++; 279 f = f | toDigit(data[j], j); 280 j++; 281 out[i] = (byte) (f & 0xFF); 282 } 283 284 return out; 285 } 286 287 /** 288 * 将十六进制字符转换成一个整数 289 * 290 * @param ch 十六进制char 291 * @param index 十六进制字符在字符数组中的位置 292 * @return 一个整数 293 * @throws RuntimeException 当ch不是一个合法的十六进制字符时,抛出运行时异常 294 */ 295 protected static int toDigit(char ch, int index) { 296 int digit = Character.digit(ch, 16); 297 if (digit == -1) { 298 throw new RuntimeException("Illegal hexadecimal character " + ch 299 + " at index " + index); 300 } 301 return digit; 302 } 303 304 /** 305 * 数字字符串转ASCII码字符串 306 * 307 * @param content 字符串 308 * @return ASCII字符串 309 */ 310 public static String StringToAsciiString(String content) { 311 String result = ""; 312 int max = content.length(); 313 for (int i = 0; i < max; i++) { 314 char c = content.charAt(i); 315 String b = Integer.toHexString(c); 316 result = result + b; 317 } 318 return result; 319 } 320 321 /** 322 * 十六进制转字符串 323 * 324 * @param hexString 十六进制字符串 325 * @param encodeType 编码类型4:Unicode,2:普通编码 326 * @return 字符串 327 */ 328 public static String hexStringToString(String hexString, int encodeType) { 329 String result = ""; 330 int max = hexString.length() / encodeType; 331 for (int i = 0; i < max; i++) { 332 char c = (char) hexStringToAlgorism(hexString 333 .substring(i * encodeType, (i + 1) * encodeType)); 334 result += c; 335 } 336 return result; 337 } 338 339 /** 340 * 十六进制字符串装十进制 341 * 342 * @param hex 十六进制字符串 343 * @return 十进制数值 344 */ 345 public static int hexStringToAlgorism(String hex) { 346 hex = hex.toUpperCase(); 347 int max = hex.length(); 348 int result = 0; 349 for (int i = max; i > 0; i--) { 350 char c = hex.charAt(i - 1); 351 int algorism = 0; 352 if (c >= '0' && c <= '9') { 353 algorism = c - '0'; 354 } else { 355 algorism = c - 55; 356 } 357 result += Math.pow(16, max - i) * algorism; 358 } 359 return result; 360 } 361 362 /** 363 * 十六转二进制 364 * 365 * @param hex 十六进制字符串 366 * @return 二进制字符串 367 */ 368 public static String hexStringToBinary(String hex) { 369 hex = hex.toUpperCase(); 370 String result = ""; 371 int max = hex.length(); 372 for (int i = 0; i < max; i++) { 373 char c = hex.charAt(i); 374 switch (c) { 375 case '0': 376 result += "0000"; 377 break; 378 case '1': 379 result += "0001"; 380 break; 381 case '2': 382 result += "0010"; 383 break; 384 case '3': 385 result += "0011"; 386 break; 387 case '4': 388 result += "0100"; 389 break; 390 case '5': 391 result += "0101"; 392 break; 393 case '6': 394 result += "0110"; 395 break; 396 case '7': 397 result += "0111"; 398 break; 399 case '8': 400 result += "1000"; 401 break; 402 case '9': 403 result += "1001"; 404 break; 405 case 'A': 406 result += "1010"; 407 break; 408 case 'B': 409 result += "1011"; 410 break; 411 case 'C': 412 result += "1100"; 413 break; 414 case 'D': 415 result += "1101"; 416 break; 417 case 'E': 418 result += "1110"; 419 break; 420 case 'F': 421 result += "1111"; 422 break; 423 } 424 } 425 return result; 426 } 427 428 /** 429 * ASCII码字符串转数字字符串 430 * 431 * @param content ASCII字符串 432 * @return 字符串 433 */ 434 public static String AsciiStringToString(String content) { 435 String result = ""; 436 int length = content.length() / 2; 437 for (int i = 0; i < length; i++) { 438 String c = content.substring(i * 2, i * 2 + 2); 439 int a = hexStringToAlgorism(c); 440 char b = (char) a; 441 String d = String.valueOf(b); 442 result += d; 443 } 444 return result; 445 } 446 447 /** 448 * 将十进制转换为指定长度的十六进制字符串 449 * 450 * @param algorism int 十进制数字 451 * @param maxLength int 转换后的十六进制字符串长度 452 * @return String 转换后的十六进制字符串 453 */ 454 public static String algorismToHexString(int algorism, int maxLength) { 455 String result = ""; 456 result = Integer.toHexString(algorism); 457 458 if (result.length() % 2 == 1) { 459 result = "0" + result; 460 } 461 return patchHexString(result.toUpperCase(), maxLength); 462 } 463 464 /** 465 * 字节数组转为普通字符串(ASCII对应的字符) 466 * 467 * @param bytearray byte[] 468 * @return String 469 */ 470 public static String byteToString(byte[] bytearray) { 471 String result = ""; 472 char temp; 473 474 int length = bytearray.length; 475 for (int i = 0; i < length; i++) { 476 temp = (char) bytearray[i]; 477 result += temp; 478 } 479 return result; 480 } 481 482 /** 483 * 二进制字符串转十进制 484 * 485 * @param binary 二进制字符串 486 * @return 十进制数值 487 */ 488 public static int binaryToAlgorism(String binary) { 489 int max = binary.length(); 490 int result = 0; 491 for (int i = max; i > 0; i--) { 492 char c = binary.charAt(i - 1); 493 int algorism = c - '0'; 494 result += Math.pow(2, max - i) * algorism; 495 } 496 return result; 497 } 498 499 /** 500 * 十进制转换为十六进制字符串 501 * 502 * @param algorism int 十进制的数字 503 * @return String 对应的十六进制字符串 504 */ 505 public static String algorismToHEXString(int algorism) { 506 String result = ""; 507 result = Integer.toHexString(algorism); 508 509 if (result.length() % 2 == 1) { 510 result = "0" + result; 511 512 } 513 result = result.toUpperCase(); 514 515 return result; 516 } 517 518 /** 519 * HEX字符串前补0,主要用于长度位数不足。 520 * 521 * @param str String 需要补充长度的十六进制字符串 522 * @param maxLength int 补充后十六进制字符串的长度 523 * @return 补充结果 524 */ 525 static public String patchHexString(String str, int maxLength) { 526 String temp = ""; 527 for (int i = 0; i < maxLength - str.length(); i++) { 528 temp = "0" + temp; 529 } 530 str = (temp + str).substring(0, maxLength); 531 return str; 532 } 533 534 /** 535 * 将一个字符串转换为int 536 * 537 * @param s String 要转换的字符串 538 * @param defaultInt int 如果出现异常,默认返回的数字 539 * @param radix int 要转换的字符串是什么进制的,如16 8 10. 540 * @return int 转换后的数字 541 */ 542 public static int parseToInt(String s, int defaultInt, int radix) { 543 int i = 0; 544 try { 545 i = Integer.parseInt(s, radix); 546 } catch (NumberFormatException ex) { 547 i = defaultInt; 548 } 549 return i; 550 } 551 552 /** 553 * 将一个十进制形式的数字字符串转换为int 554 * 555 * @param s String 要转换的字符串 556 * @param defaultInt int 如果出现异常,默认返回的数字 557 * @return int 转换后的数字 558 */ 559 public static int parseToInt(String s, int defaultInt) { 560 int i = 0; 561 try { 562 i = Integer.parseInt(s); 563 } catch (NumberFormatException ex) { 564 i = defaultInt; 565 } 566 return i; 567 } 568 569 /** 570 * 十六进制串转化为byte数组 571 * 572 * @return the array of byte 573 */ 574 public static byte[] hexToByte(String hex) 575 throws IllegalArgumentException { 576 if (hex.length() % 2 != 0) { 577 throw new IllegalArgumentException(); 578 } 579 char[] arr = hex.toCharArray(); 580 byte[] b = new byte[hex.length() / 2]; 581 for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) { 582 String swap = "" + arr[i++] + arr[i]; 583 int byteint = Integer.parseInt(swap, 16) & 0xFF; 584 b[j] = new Integer(byteint).byteValue(); 585 } 586 return b; 587 } 588 589 /** 590 * 字节数组转换为十六进制字符串 591 * 592 * @param b byte[] 需要转换的字节数组 593 * @return String 十六进制字符串 594 */ 595 public static String byteToHex(byte b[]) { 596 if (b == null) { 597 throw new IllegalArgumentException( 598 "Argument b ( byte array ) is null! "); 599 } 600 String hs = ""; 601 String stmp = ""; 602 for (int n = 0; n < b.length; n++) { 603 stmp = Integer.toHexString(b[n] & 0xff); 604 if (stmp.length() == 1) { 605 hs = hs + "0" + stmp; 606 } else { 607 hs = hs + stmp; 608 } 609 } 610 return hs.toLowerCase(); 611 //return hs.toUpperCase(); 612 } 613 614 public static byte[] subByte(byte[] input, int startIndex, int length) { 615 byte[] bt = new byte[length]; 616 for (int i = 0; i < length; i++) { 617 bt[i] = input[i + startIndex]; 618 } 619 return bt; 620 } 621 622 // 字符串转换为十六进制字符串 623 public static String stringToHexString(String s) { 624 String str = ""; 625 for (int i = 0; i < s.length(); i++) { 626 int ch = s.charAt(i); 627 String s4 = Integer.toHexString(ch); 628 str = str + s4; 629 } 630 return str; 631 } 632 633 // 十六进制字符串转换为字符串 634 public static String hexStringToString(String s) { 635 if (s == null || s.equals("")) { 636 return null; 637 } 638 s = s.replace(" ", ""); 639 byte[] baKeyword = new byte[s.length() / 2]; 640 for (int i = 0; i < baKeyword.length; i++) { 641 try { 642 baKeyword[i] = (byte) (0xff & Integer.parseInt( 643 s.substring(i * 2, i * 2 + 2), 16)); 644 } catch (Exception e) { 645 e.printStackTrace(); 646 } 647 } 648 try { 649 s = new String(baKeyword, "gbk"); 650 new String(); 651 } catch (Exception e1) { 652 e1.printStackTrace(); 653 } 654 return s; 655 } 656 657 // 十六进制字符串转换为byte[]数组 658 public static byte[] hexStringToByteArray(String s) { 659 int len = s.length(); 660 byte[] b = new byte[len / 2]; 661 for (int i = 0; i < len; i += 2) { 662 // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节 663 b[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character 664 .digit(s.charAt(i + 1), 16)); 665 } 666 return b; 667 } 668 669 // byte[]字节数组转十六进制字符串 670 public static final String bytesToHexString(byte[] bArray) { 671 StringBuffer sb = new StringBuffer(bArray.length); 672 String sTemp; 673 for (int i = 0; i < bArray.length; i++) { 674 sTemp = Integer.toHexString(0xFF & bArray[i]); 675 if (sTemp.length() < 2) 676 sb.append(0); 677 sb.append(sTemp.toUpperCase()); 678 } 679 return sb.toString(); 680 } 681 682 /** 683 * 数字(0 ~ 25) 转换为 (A ~ Z)(AA ~ AZ)(AA... ~ AA..Z) 684 * @param num 数字(0开始) 685 * @return 686 */ 687 public static String numberToLetter(Integer num) { 688 if (ObjectUtil.isNull(num)) { 689 return "Z"; 690 } 691 num = Math.abs(num); 692 if (0 == num) { 693 return "A"; 694 } 695 696 StringBuilder sb = new StringBuilder(); 697 int quotient = num / 26; // 计算商 698 int remainder = num % 26; // 计算余数 699 700 if (quotient > 0) { 701 for (int i = 0; i < quotient; i++) { 702 //char letter = (char) (65); // 将数字转换成字符,并获取其对应的ASCII码值 703 sb.append("A"); 704 } 705 } 706 if (remainder > 0) { 707 char letter = (char) (remainder + 65); // 将数字转换成字符,并获取其对应的ASCII码值 708 sb.append(Convert.toStr(letter)); 709 } 710 711 String ch = sb.toString(); 712 if (StrUtil.isNotEmpty(ch)) { 713 return ch.toUpperCase(); 714 } 715 716 return ch; 717 } 718}