This article for you to share the Java simulation of PHP pack and unpack class of specific code for your reference, the specific content as follows
Package qghl.intp.util;
Import java.io.IOException;
Import Java.io.InputStream;
public class packutil{/** * Package String * Similar to PHP in Java implementation of the pack * * @param str * @return
public static byte[] Pack (String str) {int nibbleshift = 4;
int position = 0;
int len = Str.length ()/2 + str.length ()% 2;
byte[] output = new Byte[len];
for (char V:str.tochararray ()) {Byte n = (byte) v;
if (n >= ' 0 ' && n <= ' 9 ') {n = ' 0 ');
else if (n >= ' a ' && n <= ' F ') {n-= (' A '-10);
else if (n >= ' a ' && n <= ' f ') {n-= (' A '-10);
else {continue;
} Output[position] |= (n << nibbleshift);
if (Nibbleshift = = 0) {position++;
} Nibbleshift = (Nibbleshift + 4) & 7;
return output;
/** * 16 character Decompression class PHP unpack * * @param is * @param len * @return * @throws ioexception/public static String unpack (inputstre
AM is, int len) throws IOException {byte[] bytes = new Byte[len];
Is.read (bytes);
Return unpack (bytes); /*** * 16 character Decompression class php unpack * @param bytes * * @return/public static String unpack (byte[
] {StringBuilder StringBuilder = new StringBuilder ("") bytes);
if (bytes = NULL | | bytes.length <= 0) {return null;
for (int i = 0; i < bytes.length i++) {int v = bytes[i] & 0xFF;
String HV = integer.tohexstring (v);
if (Hv.length () < 2) {stringbuilder.append (0);
} stringbuilder.append (HV);
return stringbuilder.tostring ();
}
}
The above is the entire content of this article, I hope to learn Java program to help you.