Home > Java > javaTutorial > JAVA hexadecimal and string conversion

JAVA hexadecimal and string conversion

高洛峰
Release: 2017-01-24 15:10:57
Original
2191 people have browsed it

toHexString
public static String toHexString(int i) returns the string representation of an integer parameter in the form of a hexadecimal unsigned integer.
If the parameter is negative, the unsigned integer value is the parameter plus 232; otherwise it is equal to the parameter. Converts the value to a hexadecimal (base 16) ASCII numeric string without leading 0s. If the size value of an unsigned number is zero, it is represented by a zero character '0' ('\u0030'); otherwise, the first character in the representation of the size of an unsigned number will not be a zero character. Use the following characters as hexadecimal numbers:
0123456789abcdef
The range of these characters is from '\u0030' to '\u0039' and from '\u0061' to '\u0066'. If you want uppercase letters, you can call the String.toUpperCase() method on the result:
Integer.toHexString(n).toUpperCase()
Parameters:
i - the integer to be converted to a string.
Returns:
The string representation of the unsigned integer value represented by the hexadecimal (base 16) argument.
// 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")));
}
/*
* Hexadecimal numeric character set
*/
private static String hexString="0123456789ABCDEF";
/*
* Encode the string into hexadecimal numbers, applicable to all characters (including Chinese)
*/
public static String encode(String str)
{
//Get the word according to the default encoding Section array
byte[] bytes=str.getBytes();
StringBuilder sb=new StringBuilder(bytes.length*2);
//Decompose each byte in the byte array into 2 digit hexadecimal integer
for(int i=0;i{
sb.append(hexString.charAt((bytes[i]&0xf0)>>4 ));
sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
}
return sb.toString();
}
/*
* Decode hexadecimal numbers into strings, applicable to all characters (including Chinese)
*/
public static String decode(String bytes)
{
ByteArrayOutputStream baos= new ByteArrayOutputStream(bytes.length()/2);
//Assemble each 2-digit hexadecimal integer into one byte
for(int i=0;ibaos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
return new String( baos.toByteArray());
}
Second method:
Print the specified byte array to the console in hexadecimal form

package com.nantian.iclient.atm.sdb; 

public class Util { 
public Util() { 
} 

/** 
* 将指定byte数组以16进制的形式打印到控制台 
* @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 = &#39;0&#39; + 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 = &#39;0&#39; + hex; 
} 
ret += hex.toUpperCase(); 
} 
return ret; 
} 

/** 
* 将两个ASCII字符合成一个字节; 
* 如:"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; 
} 

/** 
* 将指定字符串src,以每两个字符分割转换为16进制形式 
* 如:"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; 
} 

}
Copy after login

More JAVA hexadecimal For articles related to string conversion, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template