Tool class for common type conversion methods and tool class for type conversion
Function: Convert hexadecimal strings to byte arrays, convert character to byte, convert Blob type to byte array, convert Arabic numerals to Chinese lowercase letters
1 import java. io. bufferedInputStream; 2 import java. io. IOException; 3 import java. SQL. blob; 4 5/** 6 * common type conversion methods tool class 7 */8 public class ConvertUtil {9 10/** 11 * byte array converted to a hexadecimal string. 12 * @ param src 13 * @ return hexadecimal String 14 */15 public static String bytesToHexString (byte [] src) {16 StringBuilder stringBuilder = new StringBuilder (""); 17 if (src = null | src. length <= 0) {18 return null; 19} 20 (Int I = 0; I <src. length; I ++) {21 int v = src [I] & 0xFF; 22 String hv = Integer. toHexString (v); 23 if (hv. length () <2) {24 stringBuilder. append (0); 25} 26 stringBuilder. append (hv); 27} 28 return stringBuilder. toString (); 29} 30 31/** 32 * hexadecimal string converted to byte array 33 * @ param hexString hexadecimal string 34 * @ return 35 */36 public static byte [] hexStringToBytes (String hexString) {37 if (hexString = null | HexString. equals ("") {38 return null; 39} 40 hexString = hexString. toUpperCase (); 41 int length = hexString. length ()/2; 42 char [] hexChars = hexString. toCharArray (); 43 byte [] d = new byte [length]; 44 for (int I = 0; I <length; I ++) {45 int pos = I * 2; 46 d [I] = (byte) (charToByte (hexChars [pos]) <4 | charToByte (hexChars [pos + 1]); 47} 48 return d; 49} 50 51/** 52 * character conversion to byte 53 * @ p Aram c characters 54 * @ return 55 */56 private static byte charToByte (char c) {57 return (byte) "0123456789 ABCDEF ". indexOf (c ); 58} 59 60/** 61 * Blob type conversion to byte array 62 * @ param blob 63 * @ return 64 */65 public static byte [] blobToBytes (Blob blob) {66 BufferedInputStream is = null; 67 try {68 is = new BufferedInputStream (blob. getBinaryStream (); 69 byte [] bytes = new byte [(int) blob. length ()]; 70 int len = Bytes. length; 71 int offset = 0; 72 int read = 0; 73 74 while (offset <len & (read = is. read (bytes, offset, len-offset)> = 0) {75 offset + = read; 76} 77 return bytes; 78} catch (Exception e) {79 return null; 80} finally {81 try {82 is. close (); 83 is = null; 84} catch (IOException e) {85 return null; 86} 87} 88} 89 90/** 91 * arabic numerals convert Chinese lowercase 92 * @ param si Arabic numerals 93 * @ return Chinese lowercase strings 94 */95 public static String transition (String si) {96 String [] aa = {"", "Ten", "Hundred", "Thousand", "Ten Thousand ", "100,000", "million", "billion"}; 97 String [] bb = {"1", "2", "3 ", "4", "5", "6", "7", "8", "9"}; 98 char [] ch = si. toCharArray (); 99 int maxindex = ch. length; 100 // character conversion 101 String result = ""; 102 // two-digit Special conversion 103 if (maxindex = 2) {104 for (int I = maxindex-1, j = 0; I> = 0; I --, j ++) {105 if (ch [j]! = 48) {106 if (j = 0 & ch [j] = 49) {107 result + = aa [I]; 108} else {109 result + = bb [ch [j]-49] + aa [I]; 110} 111} 112 // Special conversion of other digits, the maximum number of digits used for the int type is 114} else {115 for (int I = maxindex-1, j = 0; I> = 0; I --, j ++) {116 if (ch [j]! = 48) {117 result + = bb [ch [j]-49] + aa [I]; 118} 119} 120 121 return result; 122} 123}View Code