Java Hex Convert To fromHex(String str)

Here you can find the source of fromHex(String str)

Description

Converts a hex string into an array of bytes.

License

Open Source License

Parameter

Parameter Description
str string, case-insensitive, eg 1F.

Return

array of bytes, least significant at index 0.

Declaration

static public byte[] fromHex(String str) throws NumberFormatException 

Method Source Code

//package com.java2s;
/*/*w  w w.ja  v a  2 s  .co m*/
JHeader, a Java library and tools for reading and editing EXIF headers.
Copyright (C) 2005 Matthew Baker
    
This program 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.
    
This program 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.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
For further information, please contact Matthew Baker on msb@safe-mail.net.
Project home page: www.sourceforge.net/jheader
*/

public class Main {
    /**
     * Converts a hex string into an array of bytes.
     *
     * @param str string, case-insensitive, eg 1F.
     * @return array of bytes, least significant at index 0.
     */
    static public byte[] fromHex(String str) throws NumberFormatException {
        char[] chars = str.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        for (int i = 0; i < chars.length; i += 2) {
            int j = i >> 1;
            int b = 0;
            for (int k = 0; k < 2; k++) {
                int ch = chars[i + k];
                switch (ch) {
                case '0':
                    b += 0;
                    break;
                case '1':
                    b += 1;
                    break;
                case '2':
                    b += 2;
                    break;
                case '3':
                    b += 3;
                    break;
                case '4':
                    b += 4;
                    break;
                case '5':
                    b += 5;
                    break;
                case '6':
                    b += 6;
                    break;
                case '7':
                    b += 7;
                    break;
                case '8':
                    b += 8;
                    break;
                case '9':
                    b += 9;
                    break;
                case 'A':
                case 'a':
                    b += 10;
                    break;
                case 'B':
                case 'b':
                    b += 11;
                    break;
                case 'C':
                case 'c':
                    b += 12;
                    break;
                case 'D':
                case 'd':
                    b += 13;
                    break;
                case 'E':
                case 'e':
                    b += 14;
                    break;
                case 'F':
                case 'f':
                    b += 15;
                    break;
                default:
                    throw new NumberFormatException("Not a hex number");
                }
                b <<= 4 * (1 - k);
            } // for (int k...
            if (b >= 128)
                bytes[j] = (byte) (128 - b);
            else
                bytes[j] = (byte) b;
        } // for (int i...
        return bytes;
    }
}

Related

  1. fromHex(String s)
  2. fromHex(String s)
  3. fromHex(String s, boolean hexIsDefault)
  4. fromHex(String src)
  5. fromHex(String str)
  6. fromHex(String string)
  7. fromHex(String text)
  8. fromHex(String[] data)
  9. fromHex2B(String src)