Java string to hexadecimal tool class Hex. java, tool class hex. java

Source: Internet
Author: User

Java string to hexadecimal tool class Hex. java, tool class hex. java

Original article: Hex. Java hexadecimal tool for conversion of java strings

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

The Hex. Java tool class for converting java strings into hexadecimal 0 xfecd .. and java strings are mutually converted! For development, the user login password will usually mad5 (salt + pwd) and then hex the data after md. This tool class achieves this effect.

/***/Package com. zuidaima. haohui. common. utils; /*** <pre> * 16 hexadecimal value and String/Byte conversion * @ author JerryLi * @ email lijian@dzs.mobi * @ data 2011-10-16 * http://blog.csdn.net/hzbigdog/article/details/6877712 ** http://www.blogjava.net/lijinglin/archive/2011/11/02/362567.html * </pre> ** @ project baidamei * @ author www.zuidaima.com * @ create 2012-11-14 5:53:14 */public class Hex {/*** convert the string to a 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. 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 ('');} retur N 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. 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 );} /*** convert bytes to a 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 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 00str 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 intint 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 ();} public static void main (String [] args) {String hex = "empty"; System. out. println (Hex. hexStr2Str (hex ));}}

 


Convert java string to hexadecimal

String str = "whale ";
Char [] chars = "0123456789 ABCDEF". 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]);
Sb. append ('');
}
System. out. println (sb. toString (). trim ());
So? But I can't get the value you want to say in any way...

Java string to hexadecimal Array

You can use toCharArray to convert it to a char array and then it's easy ....
It should be noted that the value range of byte is negative, so errors will occur if the value is greater than 128 .......
 

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.