Convert 4 hex digits to an int, and return the number of converted bytes. : Byte Read Write « File Input Output « Java






Convert 4 hex digits to an int, and return the number of converted bytes.

  
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayOutputStream;

/**
 * Library of utility methods useful in dealing with converting byte arrays to
 * and from strings of hexadecimal digits.
 * 
 * @author Craig R. McClanahan
 */

public final class HexUtils {
  // Code from Ajp11, from Apache's JServ

  // Table for HEX to DEC byte translation
  public static final int[] DEC = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1,
      -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
      -1, -1, -1, -1, -1, -1, -1, -1, -1, };





  /**
   * Convert 4 hex digits to an int, and return the number of converted bytes.
   * 
   * @param hex
   *          Byte array containing exactly four hexadecimal digits
   * 
   * @exception IllegalArgumentException
   *              if an invalid hexadecimal digit is included
   */
  public static int convert2Int(byte[] hex) {
    // Code from Ajp11, from Apache's JServ

    // assert b.length==4
    // assert valid data
    int len;
    if (hex.length < 4)
      return 0;
    if (DEC[hex[0]] < 0)
      throw new IllegalArgumentException("hexUtil.bad");
    len = DEC[hex[0]];
    len = len << 4;
    if (DEC[hex[1]] < 0)
      throw new IllegalArgumentException("hexUtil.bad");
    len += DEC[hex[1]];
    len = len << 4;
    if (DEC[hex[2]] < 0)
      throw new IllegalArgumentException("hexUtil.bad");
    len += DEC[hex[2]];
    len = len << 4;
    if (DEC[hex[3]] < 0)
      throw new IllegalArgumentException("hexUtil.bad");
    len += DEC[hex[3]];
    return len;
  }

}

   
    
  








Related examples in the same category

1.Byte Reader with FileInputStream
2.Byte Writer with FileOutputStream
3.Binary Dump OutputStream
4.Compare binary files
5.Reads bytes available from one InputStream and returns these bytes in a byte array.
6.Buffered copying between source(InputStream, Reader, String and byte[]) and destinations (OutputStream, Writer, String and byte[]).
7.Write and read compressed forms of numbers to DataOutput and DataInput interfaces.
8.Count the number of bytes written to the output stream.
9.Read and return the entire contents of the supplied file.
10.Get bytes from InputStream
11.Read file as bytesRead file as bytes