The example of this article for everyone to share the Java simulation PHP Pack and unpack class 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 the implementation of pack in PHP in Java * * @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 binary character decompression class PHP unpack * * @param is * @param len * @return * @throws IOException */public static String unpack (InputStream is, int len) throws IOException {byte[] b Ytes = new Byte[len]; Is.read (bytes); Return unpack (bytes); }/*** * 16 binary character decompression class php unpack * @param bytes * @return * * public static String unpack (byte[] bytes {StringBuilder StringBuilder = new StringBuilder (""); 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 whole content of this article, I hope that you learn Java program design has helped.
The above describes the Java simulation of PHP pack and unpack class, including the unpack aspects of the content, I hope that the PHP tutorial interested in a friend helpful.