Conversion of custom Chinese characters and ASCII values

Source: Internet
Author: User
Print the specified byte array in hexadecimal format to the console public static string tohexstring (int I) and return the string representation of an integer parameter in 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 the Code 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.