⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexarray.java

📁 dump3 morpheus 0.2.9 src
💻 JAVA
字号:
/**
 * DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
 * Copyright 2005 Alexander Gr&auml;sser<BR>
 * All Rights Reserved, http://dump3.sourceforge.net/<BR>
 * <BR>
 * This file is part of DuMP3.<BR>
 * <BR>
 * DuMP3 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later version.<BR>
 * <BR>
 * DuMP3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.<BR>
 * <BR>
 * You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.za.grasser.duplicate.util;

/**
 * This class makes getting and setting byte arrays from and to Strings easy.
 * 
 * @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
 * @version $Revision: 1.6 $
 */
public class HexArray {
  /** @modelguid {24DF90CD-6A40-4ECB-B64C-AA5EA88FDAC3} */
  private final static String HEXS = "0123456789abcdef";
  /** @modelguid {50C1597B-5536-48DD-8534-C6D65E0B6737} */
  private final static char[] HEX = HEXS.toCharArray();

  /**
   * private constructor for utility class
   */
  private HexArray() {
    super();
  }

  /**
   * @param s String
   * @return byte[]
   * @modelguid {F9BFE032-F329-46A4-8229-819C7E0B4448}
   */
  public static byte[] makeBytes(final String s) {
    if (s == null) {
      return null;
    }
    final byte[] bytes = new byte[(s.length() >> 1)];
    int b = 0;
    for (int i = 0; i < bytes.length; i++) {
      b = HEXS.indexOf(s.charAt((i << 1))) << 4 | HEXS.indexOf(s.charAt(((i << 1) + 1)));
      bytes[i] = (byte)(b & 0xFF);
    }
    return bytes;
  }

  /**
   * @param bytes byte[]
   * @return String
   * @modelguid {48F64FB0-58CE-4542-933C-8E6C497937C0}
   */
  public static String makeString(final byte[] bytes) {
    if (bytes == null) {
      return null;
    }
    final StringBuffer sb = new StringBuffer(bytes.length << 1);
    int b = 0;
    for (final byte element : bytes) {
      b = element & 0xFF;
      sb.append(HEX[b >>> 4]);
      sb.append(HEX[(b & 0x0F)]);
    }
    return sb.toString();
  }

  /**
   * @param pIn array to translate
   * @param pInOffs start of translation
   * @param pInLen length of array
   * @param pOut result array - if not null result will be in here - must be at least (pIn.length * sizeofint + pOutOffs) bytes long
   * @param pOutOffs start writing at this location
   * @param sizeofint allows for masking of high byte(s)
   * @return a new array or the pOut if not null
   */
  public static byte[] makeBytes(final int[] pIn, final int pInOffs, final int pInLen, final byte[] pOut, final int pOutOffs, final int sizeofint) {
    byte[] ret = null;
    final int outOffs;
    if (pOut != null) {
      ret = pOut;
      outOffs = pOutOffs;
    } else {
      ret = new byte[pInLen * sizeofint];
      outOffs = 0;
    }
    for (int j = 0; j < pInLen; j++) {
      for (int i = 0; i < sizeofint; i++) {
        final byte val = (byte)(pIn[(pInOffs + j)] >> (i << 3) & 0xFF);
        final int pos = outOffs + j * sizeofint + i;
        ret[pos] = val;
      }
    }
    return ret;
  }

  /**
   * @param pIn array to translate
   * @param pInOffs start of translation
   * @param pInLen length of array (must be divisible by sizeofint)
   * @param pOut result array - if not null result will be in here
   * @param pOutOffs start writing at this location
   * @param sizeofint allows for masking of high byte(s)
   * @return a new array or the pOut if not null
   */
  public static int[] makeInts(final byte[] pIn, final int pInOffs, final int pInLen, final int[] pOut, final int pOutOffs, final int sizeofint) {
    int[] ret = null;
    final int outOffs;
    if (pOut != null) {
      ret = pOut;
      outOffs = pOutOffs;
    } else {
      ret = new int[pInLen / sizeofint];
      outOffs = 0;
    }
    for (int j = 0; j < pInLen / sizeofint; j++) {
      ret[outOffs + j] = 0;
      for (int i = 0; i < sizeofint; i++) {
        ret[(outOffs + j)] |= pIn[(pInOffs + j * sizeofint + i)] << (i << 3) & 0x000000FF << (i << 3);
      }
    }
    return ret;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -