Java emulates the pack and unpack classes of PHP, packunpack_php tutorial

Source: Internet
Author: User
Tags unpack

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.