How to convert the data inside byte[] into a 16 binary string in Java

Source: Internet
Author: User

Original link: http://zhidao.baidu.com/link?url=RmLDjr4PtP_ Oue5j2pknzsvlht1k7hcch4-03y7vkxyhj0kawg01ctkhzc2ubvxqshgzl8pp60wbyulp6k15k_

The following 3 methods can convert byte[] to 16 binary strings, optionally one

/* *
* Convert byte[] to hex string. Here we can convert byte to int, then use integer.tohexstring (int)
* To convert into 16 binary strings.
* @param src byte[] Data
* @return Hex string
*/

Public static String bytestohexstring (byte[] src) {
StringBuilder StringBuilder = new StringBuilder ("");
if (src = = NULL | | src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String HV = integer.tohexstring (v);
if (Hv.length () < 2) {
stringbuilder.append (0);
}
stringbuilder.append (HV);
}
return stringbuilder.tostring ();
}

Public static String bytes2hexstring (byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = integer.tohexstring (B[i] & 0xFF);
if (hex.length () = = 1) {
hex = "0" + hex;
}
ret + = hex;
}
return ret;
}


Public static String Tohex (byte[] buffer) {
stringbuffer sb = new StringBuffer (Buffer.length * 2);
for (int i = 0; i < buffer.length; i++) {
sb.append (Character.fordigit (Buffer[i &) >> 4, +));
sb.append (Character.fordigit (Buffer[i] &));
}
return sb.tostring ();
}

How to convert the data inside byte[] into a 16 binary string in Java

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.