Java Hex Convert To fromHexString(String s)

Here you can find the source of fromHexString(String s)

Description

Convert a hex string to an unsigned byte array.

License

Open Source License

Parameter

Parameter Description
s String must have even number of characters. and be formed only of digits 0-9 A-F or a-f. No spaces, minus or plus signs.

Return

corresponding unsigned byte array. see http://mindprod.com/jgloss/unsigned.html

Declaration

public static byte[] fromHexString(String s) 

Method Source Code

//package com.java2s;
/* //w ww . ja  va 2  s  .  c  o  m
 * @(#)TestHex.java 
 * 
 * Summary: Demonstrate how to handle hex strings. 
 * 
 * Copyright: (c) 2009-2010 Roedy Green, Canadian Mind Products, http://mindprod.com 
 * 
 * Licence: This software may be copied and used freely for any purpose but military. 
 *          http://mindprod.com/contact/nonmil.html 
 * 
 * Requires: JDK 1.6+ 
 * 
 * Created with: IntelliJ IDEA IDE. 
 * 
 * Version History: 
 *  1.0 2009-06-05 - initial version. 
 */

public class Main {
    /** 
     * precomputed translate table for chars 0..'f' 
     */
    private static byte[] correspondingNibble = new byte['f' + 1];

    /** 
     * Convert a hex string to an unsigned byte array. 
     * Permits upper or lower case hex. 
     * 
     * @param s String must have even number of characters. 
     *          and be formed only of digits 0-9 A-F or 
     *          a-f. No spaces, minus or plus signs. 
     * 
     * @return corresponding unsigned byte array. see http://mindprod.com/jgloss/unsigned.html 
     */
    public static byte[] fromHexString(String s) {
        int stringLength = s.length();
        if ((stringLength & 0x1) != 0) {
            throw new IllegalArgumentException("fromHexString requires an even number of hex characters");
        }
        byte[] bytes = new byte[stringLength / 2];

        for (int i = 0, j = 0; i < stringLength; i += 2, j++) {
            int high = charToNibble(s.charAt(i));
            int low = charToNibble(s.charAt(i + 1));
            // You can store either unsigned 0..255 or signed -128..127 bytes in a byte type.  
            bytes[j] = (byte) ((high << 4) | low);
        }
        return bytes;
    }

    /** 
     * convert  a single char to corresponding nibble using a precalculated array. 
     * Based on code by: 
     * Brian Marquis 
     * Orion Group Software Engineers http://www.ogse.com 
     * 
     * @param c char to convert. must be 0-9 a-f A-F, no 
     *          spaces, plus or minus signs. 
     * 
     * @return corresponding integer  0..15 
     * @throws IllegalArgumentException on invalid c. 
     */
    private static int charToNibble(char c) {
        if (c > 'f') {
            throw new IllegalArgumentException("Invalid hex character: " + c);
        }
        int nibble = correspondingNibble[c];
        if (nibble < 0) {
            throw new IllegalArgumentException("Invalid hex character: " + c);
        }
        return nibble;
    }
}

Related

  1. fromHexString(String input)
  2. fromHexString(String input)
  3. fromHexString(String input)
  4. fromHexString(String s)
  5. fromHexString(String s)
  6. fromHexString(String s)
  7. fromHexString(String s)
  8. fromHexString(String s)
  9. fromHexString(String s)