Byte[] array with hexadecimal string and string conversion--Reprint

Source: Internet
Author: User

string conversion to hexadecimal string Method 1:

/**
* String converted to hexadecimal string
*/
public static string Str2hexstr (String str) {
Char[] chars = "0123456789ABCDEF". ToCharArray ();
StringBuilder sb = new StringBuilder ("");
byte[] bs = Str.getbytes ();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (Bs[i] & 0x0f0) >> 4;
Sb.append (Chars[bit]);
bit = Bs[i] & 0x0f;
Sb.append (Chars[bit]);
}
return sb.tostring ();
}

hexadecimal string conversion into array Method 1:

/**
* Convert 16 binary string to byte array
* @param hexstring
* @return byte[]
*/
public static byte[] Hexstringtobyte (String hex) {
int len = (hex.length ()/2);
Byte[] result = new Byte[len];
char[] Achar = Hex.tochararray ();
for (int i = 0; i < len; i++) {
int pos = i * 2;
Result[i] = (byte) (ToByte (Achar[pos]) << 4 | tobyte (Achar[pos + 1]));
}
return result;
}

private static int ToByte (char c) {
byte B = (byte) "0123456789ABCDEF". IndexOf (c);
return b;
}

Convert array to hexadecimal string Method 1:

/**
* Array converted to hexadecimal string
* @param byte[]
* @return hexstring
*/
public static final String bytestohexstring (byte[] barray) {
StringBuffer sb = new StringBuffer (barray.length);
String stemp;
for (int i = 0; i < barray.length; i++) {
Stemp = integer.tohexstring (0xFF & Barray[i]);
if (Stemp.length () < 2)
Sb.append (0);
Sb.append (Stemp.touppercase ());
}
return sb.tostring ();
}

Byte[] array into hexadecimal string method 2:

/**
* Arrays are converted to hexadecimal strings
* @param byte[]
* @return hexstring
*/
public static String toHexString1 (byte[] b) {
StringBuffer buffer = new StringBuffer ();
for (int i = 0; i < b.length; ++i) {
Buffer.append (ToHexString1 (b[i));
}
return buffer.tostring ();
}
public static String toHexString1 (Byte b) {
String s = integer.tohexstring (b & 0xFF);
if (s.length () = = 1) {
Return "0" + s;
}else{
return s;
}
}

Hex string Conversion string Method 1:

/**
* Hexadecimal string converted to string
* @param hexstring
* @return String
*/
public static string Hexstr2str (String hexstr) {

        String str = "0123456789ABCDEF";   
         char[] Hexs = Hexstr.tochararray ();   
         byte[] bytes = new Byte[hexstr.length ()/2];  
        int n;& nbsp; 
        for (int i = 0; i < bytes.length; i++) {  
& nbsp;           n = str.indexof (hexs[2 * i]) * 16;&NBSP;&NBSP;
& nbsp;           n + = str.indexof (hexs[2 * i + 1]);   
& nbsp;           bytes[i] = (byte) (n & 0xff);   
       }  
        return new String (bytes);   
   }

Hex string Conversion string Method 2:

/**
* Hexadecimal string Conversion string
* @param hexstring
* @return String
*/
public static string Tostringhex (string s) {
byte[] Bakeyword = new Byte[s.length ()/2];
for (int i = 0; i < bakeyword.length; i++) {
try {
Bakeyword[i] = (byte) (0xFF & Integer.parseint (s.substring (
I * 2, I * 2 + 2), 16));
} catch (Exception e) {
E.printstacktrace ();
}
}
try {
s = new String (Bakeyword, "utf-8");//Utf-16le:not
} catch (Exception E1) {
E1.printstacktrace ();
}
return s;
}

Reprinted from http://hanyiwto.blog.163.com/blog/static/1817608420100202373316/

Byte[] array with hexadecimal string and string conversion--Reprint

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.