Java string to 16 binary tool class Hex.java

Source: Internet
Author: User

Original: Java string to 16 binary tool class Hex.java

Source code: Http://www.zuidaima.com/share/1550463378410496.htm

Java string to 16 binary tool class Hex.java implementation 16 binary 0XFECD. And the Java string to the mutual conversion! If you do development, usually the user login password will be mad5 (salt + pwd) and then the MD after the data hex. This tool class is to achieve this effect.

    /* * */package com.zuidaima.haohui.common.utils;/** * <pre> * 16 conversion between binary value and String/byte * @author Jerryli * @emai l [email protected] * @data 2011-10-16 * http://blog.csdn.net/hzbigdog/article/details/6877712 * * Http://www.blog java.net/lijinglin/archive/2011/11/02/362567.html * </pre> * * @project Baidamei * @author www.zuidaima.com * @crea  Te 2012-11-14 pm 5:53:14 */public class Hex {/** * string converted to hexadecimal string * * @param string * str to convert ASCII String * @return The string is separated by a space between bytes, such as: [6C 6B] */public static string str2hexstr (String str) {char[] chars = "0123456789ABCDEF". ToChar Array (); 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]); Sb.append (' ');} Return sb.tostring (). Trim ();} /** * 16 Binary conversion String * * @param string * str byte string (no delimiter between byte: [616c6b]) * @return string corresponding to */public sTatic string Hexstr2str (String hexstr) {string str = "0123456789ABCDEF"; char[] Hexs = Hexstr.tochararray (); 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);}  /** * bytes Convert to hexadecimal string * * @param byte[] b byte array * @return string each byte value is separated by a space */public static string Byte2hexstr (byte[] b) {String stmp = ""; StringBuilder sb = new StringBuilder (""); 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 converted to byte value * * @param string * src byte string, no delimiter between each byte * @return byte[] */public static byte[] H Exstr2bytes (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;}  The string/** * string is converted to a Unicode string * * @param string * strText Full-width String * @return string with no delimiter 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.tohex String (INTASC), if (Intasc > +) str.append ("\\u" + strhex), else//low in front complement 00str.append ("\\u00" + Strhex);} return str.tostring ();} /** * Unicode string converted to Strings * * @param string * Hex 16 Binary Value string (one Unicode 2byte) * @return string Full-width strings * * 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);//High Need to make up 00 re-turn string S1 = s.substring (2, 4) + "00";//low direct-to-string s2 = s.substring (4);//Convert the 16 binary string to Intint n = integer.valueof (S1, +) + integer.valueof (S2, 16);//int to character char[] chars = Character.tocha RS (n); Str.append (new String (chars));} return str.tostring ();} public static void Main (string[] args) {String hex = "Ef2c71b29202f3e642f2abd8d518f367ec3fbf6a6a61beb678ae0c871ee368ac "; System.out.println (Hex.hexstr2str (Hex));}}

Java string to 16 binary tool class Hex.java

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.