Tohexstring
The public static string tohexstring (int i) returns the string representation of an integer argument in the form of a 16 unsigned integer.
If the argument is negative, then the unsigned integer value is the argument plus 232, otherwise equal to the argument. Converts the value to an ASCII numeric string without a leading 0 in the 16 (Radix 16) system. If the size value of the unsigned number is zero, it is represented by a 0 character ' 0 ' (' \u0030 '), otherwise the first character in the unsigned size representation will not be 0 characters. Use the following characters as hexadecimal digits:
0123456789abcdef
The range of these characters is from ' \u0030 ' to ' \u0039 ' and from ' \u0061 ' to ' \u0066 '. If you want to get uppercase letters, you can invoke the String.touppercase () method on the result:
Integer.tohexstring (n). toUpperCase ()
Parameters:
I-an integer to be converted into a string.
Return:
The string representation of the unsigned integer value represented by the hexadecimal (cardinality 16) parameter.
Convert 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 hexadecimal encoding to 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 hexadecimal encoding to 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"));
}
/*
* 16 Digital Character Set
*/
private static String hexstring= "0123456789ABCDEF";
/*
* Encode the string into 16 digits, applicable to all characters (including Chinese)
*/
public static string encode (String str)
{
Gets a byte array based on the default encoding
Byte[] Bytes=str.getbytes ();
StringBuilder sb=new StringBuilder (bytes.length*2);
Splits each byte in a byte array into a 2-bit 16-digit integer
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 ();
}
/*
* Decode 16 binary digits to a string and apply to all characters (including Chinese)
*/
public static string decode (string bytes)
{
Bytearrayoutputstream baos=new Bytearrayoutputstream (Bytes.length ()/2);
Assembles every 2-bit 16-digit integer 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 ());
}
The second method:
Prints the specified byte array to the console in 16-binary form
Copy Code code as follows:
Package com.nantian.iclient.atm.sdb;
public class Util {
Public Util () {
}
/**
* Print the specified byte array to the console in 16 form
* @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;
}
/**
* Match two ASCII characters into one byte;
* such as: "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;
}
/**
* The specified string src is converted to 16 in every two characters
* such as: "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;
}
}