Java hex hexadecimal byte String Conversion class

Source: Internet
Author: User

For work needs, we sorted out a hexadecimal byte string value conversion class.

If you need it, you can use it directly.

Package MoBi. DZS. util; /*** conversion between hexadecimal value and string/byte * @ author jerryli * @ email lijian@dzs.mobi * @ data 2011-10-16 **/public class chexconver {/*** String Conversion hexadecimal string * @ Param string str ascii string to be converted * @ return string each byte is separated by a space, for example: [61 6C 6B] */public static string str2hexstr (string Str) {char [] chars = "0123456789 abcdef ". tochararray (); stringbuilder sb = new stringbuilder (""); byte [] BS = Str. getbyte S (); 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]); sb. append ('');} return sb. tostring (). trim ();}/*** hexadecimal conversion string * @ Param string STR byte string (no separator between byte, for example, [616c6b]) * @ return string */public static string hexstr2str (string hexstr) {string STR = "0123456789 abcdef"; char [] hexs = hexstr. tocharar Ray (); byte [] bytes = new byte [hexstr. length ()/2]; int N; For (INT I = 0; I <bytes. length; I ++) {n = Str. indexof (hexs [2 * I]) * 16; n + = Str. indexof (hexs [2 * I + 1]); bytes [I] = (byte) (N & 0xff);} return new string (bytes );} /*** convert bytes to a hexadecimal string * @ Param byte [] B byte array * @ return string separate each byte value by Space */public static string byte2hexstr (byte [] b) {string stmp = ""; stringbuilder sb = new stri Ngbuilder (""); For (INT n = 0; n <B. length; n ++) {stmp = integer. tohexstring (B [N] & 0xff); sb. append (stmp. length () = 1 )? "0" + stmp: stmp); sb. append ("");} return sb. tostring (). touppercase (). trim ();}/*** bytes string to byte value * @ Param string SRC byte string, no separator * @ return byte [] */public static byte [] hexstr2bytes (string SRC) {int m = 0, n = 0; int L = SRC. length ()/2; system. out. println (l); byte [] ret = new byte [l]; for (INT I = 0; I <L; I ++) {M = I * 2 + 1; N = m + 1; RET [I] = byte. decode ("0x" + SRC. substring (I * 2, m) + SRC. substring (m, n);} return ret ;} /*** convert string to Unicode string * @ Param string strtext full-width string * @ return string no separator between each Unicode * @ throws exception */public static string strtounicode (string strtext) throws exception {char C; stringbuilder STR = new stringbuilder (); int intasc; string strhex; For (INT I = 0; I <strtext. length (); I ++) {c = strtext. charat (I); intasc = (INT) C; strhex = integer. tohexstring (intasc); If (intasc> 128) Str. append ("\ U" + strhex); else // Add 00 STR in front of the low position. append ("\ u00" + strhex);} return Str. tostring ();}/*** convert Unicode strings to string strings * @ Param string hex hexadecimal value string (a Unicode is 2 bytes) * @ return string full-width string */public static string unicodetostring (string HEX) {int T = hex. length ()/6; stringbuilder STR = new stringbuilder (); For (INT I = 0; I <t; I ++) {string S = hex. substring (I * 6, (I + 1) * 6); // you need to add 00 to the high position and then convert it to string S1 = S. substring (2, 4) + "00"; // string S2 = S. substring (4); // convert string in hexadecimal format to int n = integer. valueof (S1, 16) + integer. valueof (S2, 16); // convert int to Char [] chars = character. tochars (n); Str. append (new string (chars);} return Str. tostring ();}}
Related Article

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.