Conversion between JAVA Hex and string, and java hex

Source: Internet
Author: User

Conversion between JAVA Hex and string, and java hex
ToHexString
Public static String toHexString (int I) returns the String representation of an integer parameter in the hexadecimal notation.
If the parameter is negative, the unsigned integer is 232. Otherwise, the value is equal to the parameter. Convert the value to an ASCII numeric string without leading 0 in hexadecimal notation (base 16. If the value of the unsigned number is zero, it is expressed with a zero character '0' ('\ u0030'); otherwise, the first character in the expression of the unsigned number size is not a zero character. Use the following characters as hexadecimal numbers:
0123456789 abcdef
These characters range from '\ u0030' to' \ u0039 'and from' \ u0061 'to' \ u0066 '. To obtain uppercase letters, you can call the String. toUpperCase () method in the result:
Integer. toHexString (n). toUpperCase ()
Parameters:
I-The integer to be converted to a string.
Return Value:
The string representation of the unsigned integer represented by the hexadecimal (base 16) parameter.
// Convert the string to hexadecimal encoding.
Public static String toHexString (String s)
{
String str = "";
For (int I = 0; I <s. length (); I ++)
{
Int ch = (int) s. charAt (I );
String s4 = Integer. toHexString (ch );
Str = str + s4;
}
Return str;
}
// Convert the hexadecimal encoded 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;
}
// Convert the hexadecimal encoded 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;
}
Public static void main (String [] args ){
System. out. println (encode ("Chinese "));
System. out. println (decode (encode ("Chinese ")));
}
/*
* Hexadecimal numeric character set
*/
Private static String hexString = "0123456789 ABCDEF ";
/*
* Encodes a string into a hexadecimal number. It is applicable to all characters (including Chinese characters)
*/
Public static String encode (String str)
{
// Obtain the byte array based on the default encoding.
Byte [] bytes = str. getBytes ();
StringBuilder sb = new StringBuilder (bytes. length * 2 );
// Split each byte in the byte array into two hexadecimal integers.
For (int I = 0; I <bytes. length; I ++)
{
Sb. append (hexString. charAt (bytes [I] & 0xf0)> 4 ));
Sb. append (hexString. charAt (bytes [I] & 0x0f)> 0 ));
}
Return sb. toString ();
}
/*
* Decodes a hexadecimal number into a string and applies to all characters (including Chinese)
*/
Public static String decode (String bytes)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream (bytes. length ()/2 );
// Assemble every two hexadecimal integers into one byte
For (int I = 0; I <bytes. length (); I + = 2)
Baos. write (hexString. indexOf (bytes. charAt (I) <4 | hexString. indexOf (bytes. charAt (I + 1 ))));
Return new String (baos. toByteArray ());
}
Method 2:
Print the specified byte array in hexadecimal format to the console
Copy codeThe Code is as follows:
Package com. nantian. iclient. atm. sdb;

Public class Util {
Public Util (){
}

/**
* Print the specified byte array in hexadecimal format to the console.
* @ Param hint String
* @ Param B byte []
* @ Return void
*/
Public static void printHexString (String hint, byte [] B ){
System. out. print (hint );
For (int I = 0; I <B. length; I ++ ){
String hex = Integer. toHexString (B [I] & 0xFF );
If (hex. length () = 1 ){
Hex = '0' + hex;
}
System. out. print (hex. toUpperCase () + "");
}
System. out. println ("");
}

/**
*
* @ Param B byte []
* @ Return String
*/
Public static String Bytes2HexString (byte [] B ){
String ret = "";
For (int I = 0; I <B. length; I ++ ){
String hex = Integer. toHexString (B [I] & 0xFF );
If (hex. length () = 1 ){
Hex = '0' + hex;
}
Ret + = hex. toUpperCase ();
}
Return ret;
}

/**
* Combine two ASCII characters into one byte;
* For example, "EF" --> 0xEF
* @ Param src0 byte
* @ Param src1 byte
* @ Return byte
*/
Public static byte uniteBytes (byte src0, byte src1 ){
Byte _ b0 = Byte. decode ("0x" + new String (new byte [] {src0}). byteValue ();
_ B0 = (byte) (_ b0 <4 );
Byte _ b1 = Byte. decode ("0x" + new String (new byte [] {src1}). byteValue ();
Byte ret = (byte) (_ b0 ^ _ b1 );
Return ret;
}

/**
* Separates the specified string src and converts the string to hexadecimal form.
* For example: "2B44EFD9" --> byte [] {0x2B, 0x44, 0xEF, 0xD9}
* @ Param src String
* @ Return byte []
*/
Public static byte [] HexString2Bytes (String src ){
Byte [] ret = new byte [8];
Byte [] tmp = src. getBytes ();
For (int I = 0; I <8; I ++ ){
Ret [I] = uniteBytes (tmp [I * 2], tmp [I * 2 + 1]);
}
Return ret;
}

}

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.