All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.HexUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import java.util.regex.Pattern;

//------------------------------------------------------------------------------
/**
 * General hexadecimal utility functions.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class HexUtil
{
   private static final Pattern sHexPattern = Pattern.compile("#?[0-9A-F]+", Pattern.CASE_INSENSITIVE);

   //---------------------------------------------------------------------------
   /**
    Returns whether or not the specified String could be a hexidecimal number.
    */
   public static boolean isHex(String inValue)
   {
      boolean result = false;
      if (StringUtil.isSet(inValue))
      {
         result = sHexPattern.matcher(inValue).matches();
      }

      return result;
   }

   //---------------------------------------------------------------------------
   public static byte[] hexStringToByteArray(String inHexString)
   {
      int len = inHexString.length();
      byte[] data = new byte[len / 2];
      for (int i = 0; i < len; i += 2)
      {
         data[i / 2] = (byte) ((Character.digit(inHexString.charAt(i), 16) << 4)
                               + Character.digit(inHexString.charAt(i + 1), 16));
      }

      return data;
   }

   //---------------------------------------------------------------------------
   public static String byteArrayToHexString(byte[] inValue)
   {
      StringBuilder result = new StringBuilder();
      for (int i = 0; i < inValue.length; i++)
      {
         result.append(Integer.toString( ( inValue[i] & 0xff ) + 0x100, 16).substring( 1 ));
      }

      return result.toString();
   }

   //---------------------------------------------------------------------------
   public static String byteToHexString(byte inValue)
   {
      String value = Integer.toHexString(inValue).toUpperCase();
      if (1 == value.length())
      {
         value = "0" + value;
      }

      return value;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy