Java simulates PHP's pack and unpack classes, Packunpack
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.
Articles you may be interested in:
- Python Struct.unpack
- PHP Pack and unpack character meaning
- Parsing binary Stream Interface Application Example Pack, unpack, Ord function use method
- How PHP uses pack to process binary files
- Java Foundation (Package)
http://www.bkjia.com/PHPjc/1119958.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119958.html techarticle Java Simulation PHP Pack and unpack class, packunpack This example for you to share the Java simulation PHP Pack and unpack class of specific code for your reference, the specific contents of the following package qghl.intp ...