JAVA hexadecimal and string conversions __java

Source: Internet
Author: User

The

Prints the specified byte array in 16 to the console
tohexstring
public static string tohexstring (int i) returns the string representation of an integer parameter as a 16-way 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 ()
Parameter:
I-an integer to be converted to a string.
Returns:
A string representation of an unsigned integer value represented by a hexadecimal (radix 16) parameter.

The conversion string is hexadecimal-encoded 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 (); 
The 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, apply to all characters (including Chinese)/public static string encode (String str) {//Get byte array based on default encoding byte[] Bytes=str.getbyt 
ES (); 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) > 
&GT;4)); 
Sb.append (Hexstring.charat (bytes[i]&0x0f) >>0)); 
return sb.tostring (); * * * to decode 16 binary digits to a string, applicable to all characters (including Chinese)/public static string decode (string bytes) {Bytearrayoutputstream baos=new byt 
Earrayoutputstream (Bytes.length ()/2); Assembles each 2-bit 16-digit integer into a 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: print the specified byte array to the console in the form of 16 the copy Code code is as follows: Package com.nantian.iclient.atm.sdb; public class Util {public Util () {}/** * Prints the specified byte array to the console * @param hint String * @param b byte[] * @retu 
RN 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; /** * Matches two ASCII characters into one byte; * such as: "EF"--> 0xEF * @param src0 byte * @param src1 byte * @return byte/public stat 
IC 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; /** * will specify string src, converted to 16 in every two characters * such as: "2b44efd9"--> byte[]{0x2b, 0x44, 0xEF, 0xd9} * @param src String * @retu RN 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.