Java Base Convert fromBase16toByteArray(String bytes)

Here you can find the source of fromBase16toByteArray(String bytes)

Description

This method return a byte[] from a String.

License

Apache License

Parameter

Parameter Description
bytes the String representing the bytes in hex form

Return

the formatted bytes

Declaration

public static byte[] fromBase16toByteArray(String bytes) 

Method Source Code

//package com.java2s;
/*//www  .j a  v a2  s.c o m
   Copyright 2008-2013 Andrew Rapp, http://code.google.com/p/xbee-api/
    
   Copyright 2008-2013 ITACA-TSB, http://www.tsb.upv.es/
   Instituto Tecnologico de Aplicaciones de Comunicacion 
   Avanzadas - Grupo Tecnologias para la Salud y el 
   Bienestar (TSB)
    
    
   See the NOTICE file distributed with this work for additional 
   information regarding copyright ownership
    
   Licensed 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.
*/

public class Main {
    /**
     * This method return a <code>byte[]</code> from a <code>String</code>. It support the format<br>
     * of the {@link #toBase16(byte[])} and in general it supports the following {@link Pattern}:<br>
     * <pre>\s*((0x[0-9a-f]{2}|[0-9a-f]{2})\s*)+</pre>
     * <b>Exmaple:</b>
     * <pre>
     * 0x23 0xab 0xfE 0xDD
     * 0x23 0xab 0xfe 0xdd
     * 0x23 ab 0xfE DD
     * </pre>  
     * <b>NOTE</b><br>
     * The main difference with {@link #fromBase16toIntArray(String)} is that the data returned<br>
     * is signed, so values goes from <code>-128 to 127</code>
     * 
     * @param bytes the String representing the bytes in hex form
     * @return the formatted bytes
     * @since 0.6.0 - Revision 60 
     */
    public static byte[] fromBase16toByteArray(String bytes) {
        int[] values = fromBase16toIntArray(bytes);
        byte[] returns = new byte[values.length];
        for (int i = 0; i < values.length; i++) {
            returns[i] = (byte) (values[i] & 0xFF);
        }
        return returns;
    }

    /**
     * This method convert string that matches the following formats:<ul>
     * <li>0xfe 0x19 0x44 0x81 0x00</li> 
     * <li>fe 19 44 81 00</li>
     * </ul>
     * to the following array of int<br>
     * [ 254, 25, 68, 129, 00 ]
     * 
     * @param bytes the {@link String} representing the bytes in hex form (as described above)
     * @return the int array parsed from the argument
     * @since 0.6.0 - Revision 60
     */
    public static int[] fromBase16toIntArray(String bytes) {
        final String PATTERN = "\\s*((0x[0-9a-f]{2}|[0-9a-f]{2})\\s*)+";
        bytes = bytes.toLowerCase();
        if (bytes.matches(PATTERN) == false) {
            throw new IllegalArgumentException("Unable to parse " + bytes + " doesn't match regex " + PATTERN);
        }
        String[] singleBytes = bytes.split("\\s+");
        String item;
        int[] values = new int[singleBytes.length];
        for (int i = 0; i < singleBytes.length; i++) {
            item = singleBytes[i];
            if (item.length() == 0)
                continue;

            if (item.startsWith("0x")) {
                item = item.substring(2);
            }

            values[i] = (Integer.parseInt(item, 16) & 0xFF);
        }
        return values;
    }
}

Related

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