Objective
Java and other languages written by the program for TCP/IP socket communication, communication content is generally converted into byte an array, Java in the character and array conversion is also very convenient. Here, come with me. byte the principle of conversion between strings and
Principle
As we all know, the type in Java byte occupies 1 bytes, 8 bits, and 16 characters occupy 4 bits, so each byte can be represented by two characters, and vice versa.
As an example
In binary notation:0111 1011
Every 4 digits are represented by characters:7 b
Yes, the principle is simple, and then it's implemented in code:
byte[] ext. 16 binary string
Method One
Thinking: First convert the byte[] dimension char[] , then char[] convert to a string
public static String Bytes2hex (byte[] src) {
if (src = null | | src.length <= 0) {return
null;
}
char[] res = new char[src.length * 2]; Each byte corresponds to two characters
final char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '};
for (int i = 0, j = 0; i < src.length i++) {
res[j++] = hexdigits[src[i] >> 4 & 0x0f]; >res[j++] = Hexdigits[src[i] & 0x0f]; Save byte lower 4-bit
} return
new String (res);
Method Two
Idea: First byte convert to int type, then convert to string
public static String Bytestohex (byte[] src) {
if (src = null | | src.length <= 0) {return
null;
}
StringBuilder StringBuilder = new StringBuilder ("");
for (int i = 0; i < src.length i++) {
//is byte and 0xff phase, because int is 32 bits, and after 0xFF and then discard the preceding 24 bits, only the 8-bit
String str = inte Ger.tohexstring (Src[i] & 0xff);
if (Str.length () < 2) {//less than two digits to complement 0
stringbuilder.append (0);
}
Stringbuilder.append (str);
return stringbuilder.tostring ();
}
16 feed string to byte[]
Thinking: First convert the string to char[] , and then convert tobyte[]
public static byte[] Hextobytes (String hexstring) {
if (hexstring = = NULL | | hexstring.equals ("")) {return
null;< c3/>}
int length = Hexstring.length ()/2;
char[] Hexchars = Hexstring.tochararray ();
byte[] bytes = new Byte[length];
String hexdigits = "0123456789abcdef";
for (int i = 0; i < length; i++) {
int pos = i * 2;//two characters correspond to one byte
int h = hexdigits.indexof (Hexchars[pos]) & lt;< 4; Note 1
int l = hexdigits.indexof (Hexchars[pos + 1]);/Note 2
if (h = = 1 | | l = = = 1) {//non-16-character return
null;< c14/>}
Bytes[i] = (byte) (H | l);
}
return bytes;
}
Note: Note 1 is obtained xxxx0000 , note 2 is obtained 0000xxxx , or the two characters are converted to one byte .
Let me give you an example.
MD5 encryption
public static string Getmd5byfile (file file) {
string ret= null;
FileInputStream FIS = null;
try {
fis = new FileInputStream (file);
MessageDigest MD = messagedigest.getinstance ("MD5");
byte[] buffer = new byte[1024];
int Len;
while (len = fis.read (buffer) > 0) {
md.update (buffer, 0, Len);
}
ret = Bytes2hex (Md.digest ()); Converts MD5 encrypted byte[] to string
} catch (Exception e) {
e.printstacktrace ();
} finally {
if (FIS!= null) { c16/>try {
fis.close ();
} catch (IOException e) {
e.printstacktrace ();
}
}} return ret;
}
Summarize
Well, it should be understood, but it is not difficult. The above is the entire content of this article, I hope this article content for everyone's study or work can bring certain help, if there are questions you can message exchange.