Java Base Convert fromBase16(String data)

Here you can find the source of fromBase16(String data)

Description

from Base

License

Open Source License

Declaration

public static byte[] fromBase16(String data) 

Method Source Code

//package com.java2s;
/*  Copyright (C) 2009 Mobile Sorcery AB
    //from   w  ww  . ja  v  a2  s .com
This program is free software; you can redistribute it and/or modify it
under the terms of the Eclipse Public License v1.0.
    
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 Eclipse Public License v1.0 for
more details.
    
You should have received a copy of the Eclipse Public License v1.0 along
with this program. It is also available at http://www.eclipse.org/legal/epl-v10.html
*/

public class Main {
    public static byte[] fromBase16(String data) {
        boolean extraByte = data.length() % 2 == 1;
        byte[] result = new byte[data.length() / 2 + (extraByte ? 1 : 0)];

        for (int i = 0; i < result.length; i++) {
            int value = Integer.parseInt(data.substring(2 * i, Math.min(2 * i + 2, data.length())), 16);
            result[i] = (byte) (value & 0xff);
        }

        return result;
    }
}

Related

  1. fromBase(String num, String baseChars)
  2. fromBase10(final long base10)
  3. fromBase16toByteArray(String bytes)
  4. fromBase16toIntArray(String bytes)
  5. fromBase26(String number)
  6. fromBase36(String base36)