Java Simulation PHP Pack and unpack class _php tips

Source: Internet
Author: User
Tags pack unpack

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.

Related Article

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.