Conversion of JAVA hexadecimal to string

Source: Internet
Author: User

Http://www.jb51.net/article/18146.htm The original link specifies that a byte array is printed to the console as a 16 binary tohexstring
public static string tohexstring (int i) returns the string representation of an integer parameter as an unsigned 16-based integer.
If the argument is negative, then the unsigned integer value is the parameter plus 232, otherwise equal to the parameter. Converts the value to a 16-based (radix 16) ASCII numeric string without a leading 0. 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 representation of the unsigned number size 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 call the String.touppercase () method on the result:
Integer.tohexstring (n). toUpperCase ()
Parameters:
I-an integer to convert to a string.
Return:
The string representation of an unsigned integer value represented by the hexadecimal (radix 16) parameter.
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 ();
}
}
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 binary numeric Character set
*/
private static String hexstring= "0123456789ABCDEF";
/*
* Encode a string into 16 digits for all characters (including Chinese)
*/
public static string encode (String str)
{
Get byte array by 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-decimal 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 ();
}
/*
* Decodes 16 binary digits into a string for all characters (including Chinese)
*/
public static string decode (string bytes)
{
Bytearrayoutputstream baos=new Bytearrayoutputstream (Bytes.length ()/2);
Assemble every 2 bits of 16 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 ());
}
The second method:
Prints the specified byte array to the console in 16-binary formCopy CodeThe code is as follows:
Package com.nantian.iclient.atm.sdb;

public class Util {
Public Util () {
}

/**
* Prints the specified byte array to the console in 16-binary format
* @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;
* 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;
}

/**
* The specified string src, converted to 16 binary form per two character split
* 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;
}

}

Conversion of JAVA hexadecimal to string

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.