Byte[] and 16 binary strings in Java

Source: Internet
Author: User

Convert byte[] to 16 binary string

public static String Byte2hex (byte[] b) {
    StringBuilder hs = new StringBuilder ();
String stmp;
for (int n = 0; b! = null && n < b.length; n++) {
Stmp = Integer.tohexstring (B[n] & 0XFF);
if (stmp.length () = = 1)
Hs.append (' 0 ');
Hs.append (STMP);
}
Return hs.tostring (). toUpperCase ();
}



Convert 16 binary string to byte[]
public static byte[] Tobytes (String str) {
if (str = = NULL | | Str.trim (). Equals ("")) {
return new byte[0];
}

byte[] bytes = new Byte[str.length ()/2];
for (int i = 0; i < str.length ()/2; i++) {
String subStr = str.substring (i * 2, I * 2 + 2);
Bytes[i] = (byte) integer.parseint (SUBSTR, 16);
}

return bytes;
}



Ref: 69939307

Byte[] and 16 binary strings 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.