Java byte to hex

Source: Internet
Author: User

Java byte to hex 16

 PackageCom.longtop.client.codec.encryp; Public classHextransfer {/*** Converts a byte array to a string representing 16 binary values, such as: byte[]{8,18} to: 0813, and public static byte[] * Hexstr2bytearr (string strin) mutually reversible conversions Process * *@paramARRB * Byte array required for conversion *@returnconverted String *@throwsException * This method does not handle any exceptions, all exceptions are thrown*/     Public StaticString Bytearr2hexstr (byte[] ARRB) {        intIlen =arrb.length; //each byte is represented by two characters, so the length of the string is twice times the length of the arrayStringBuffer SB =NewStringBuffer (Ilen * 2);  for(inti = 0; i < Ilen; i++) {            intInttmp =Arrb[i]; //convert negative numbers to positive numbers             while(Inttmp < 0) {inttmp= Inttmp + 256; }            //numbers less than 0F need to be preceded by 0            if(Inttmp < 16) {sb.append ("0"); } sb.append (Integer.tostring (inttmp,16)); }        returnsb.tostring (); }    /*** Converts a string representing 16 binary values to a byte array, and public static string Bytearr2hexstr (byte[] arrb) * Reciprocal conversion process * *@paramStrin * The string to convert *@returnconverted byte array *@throwsException * This method does not handle any exceptions, all exceptions are thrown *@author<a href= "Mailto:[email protected]" >LiGuoQing</a>*/     Public Static byte[] Hexstr2bytearr (String strin) {byte[] ARRB =strin.getbytes (); intIlen =arrb.length; //two characters represent one byte, so the length of the byte array is the length of the string divided by 2        byte[] Arrout =New byte[ILEN/2];  for(inti = 0; i < Ilen; i = i + 2) {String strtmp=NewString (ARRB, I, 2); Arrout[i/2] = (byte) Integer.parseint (strtmp, 16); }        returnArrout; }}

Java byte to hex

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.