Common type conversion Method tool class

Source: Internet
Author: User

Function: hexadecimal string with byte array, character to Byte, blob type to byte array, Arabic to Chinese lowercase

1 ImportJava.io.BufferedInputStream;2 Importjava.io.IOException;3 ImportJava.sql.Blob;4 5 /**6 * Common type conversion method tool class7  */8  Public classConvertutil {9     Ten    /** One * The byte array is converted to a hexadecimal string. A     *  @paramsrc -     *  @returnhexadecimal string -     */    the     Public StaticString bytestohexstring (byte[] src) { -StringBuilder StringBuilder =NewStringBuilder (""); -        if(src = =NULL|| Src.length <= 0) { -            return NULL; +        } -         for(inti = 0; i < src.length; i++) { +            intv = src[i] & 0xFF; AString HV =integer.tohexstring (v); at            if(Hv.length () < 2) { -Stringbuilder.append (0); -            } - stringbuilder.append (HV); -        } -        returnstringbuilder.tostring (); in    } -     to    /** + * hexadecimal string converted to byte array -     *  @paramhexstring hexadecimal string the     *  @return  *     */ $     Public Static byte[] hexstringtobytes (String hexstring) {Panax Notoginseng        if(HexString = =NULL|| Hexstring.equals ("")) { -            return NULL; the        } +HexString =hexstring.touppercase (); A        intLength = Hexstring.length ()/2; the        Char[] Hexchars =Hexstring.tochararray (); +        byte[] D =New byte[length]; -         for(inti = 0; i < length; i++) { $            intpos = i * 2; $D[i] = (byte) (Chartobyte (Hexchars[pos]) << 4 | chartobyte (Hexchars[pos + 1])); -        } -        returnD; the    } -    Wuyi    /** the * Characters converted to bytes -     * @paramC character Wu     * @return -     */ About    Private Static byteChartobyte (Charc) { $        return(byte) "0123456789ABCDEF". IndexOf (c); -    } -     -    /** A * blob type converted to byte array +     * @paramblob the     * @return -     */ $      Public Static byte[] blobtobytes (blob blob) { theBufferedinputstream is =NULL; the         Try { theis =NewBufferedinputstream (Blob.getbinarystream ()); the             byte[] bytes =New byte[(int) Blob.length ()]; -             intLen =bytes.length; in             intOffset = 0; the             intRead = 0; the      About              while(Offset < len && (read = is.read (bytes, offset, len-offset)) >= 0) { theOffset + =Read; the             } the             returnbytes; +}Catch(Exception e) { -             return NULL; the}finally {Bayi             Try { the is.close (); theis =NULL; -}Catch(IOException e) { -                 return NULL; the             } the         } the     } the      -     /** the * Arabic numerals to Chinese lowercase the      * @paramsi Arabic numerals the      * @returnChinese lowercase string94      */ the      Public Staticstring Transition (string si) { theString []aa={"", "ten", "Hundred", "thousand", "Million", "100,000", "Million", "tens", "billion", "1 billion"};  theString []bb={"One", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; 98         Char[] Ch=Si.tochararray ();  About         intmaxindex=ch.length; -         //Conversion of characters101String result = "";102         //Special conversions for two-bit numbers103         if(maxindex==2){   104              for(intI=maxindex-1,j=0;i>=0;i--, J + +){     the                 if(ch[j]!=48){     106                     if(j==0&&ch[j]==49){      107Result + =Aa[i]; 108}Else{      109Result + = bb[ch[j]-49]+Aa[i];  the                     }    111                 }    the             }113         //special conversions for other digits, using the largest number of digits of type int of 1 billion the}Else{    the              for(intI=maxindex-1,j=0;i>=0;i--, J + +){     the                 if(ch[j]!=48){     117Result + = bb[ch[j]-49]+Aa[i]; 118                 }   119             }   -         }121         122         returnresult;123     } 124}
View Code

Common type conversion Method tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.