Java Base Convert fromBase16toIntArray(String bytes)

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

Description

from Baseto Int Array

License

Apache License

Parameter

Parameter Description
bytes the String representing the bytes in hex form

Return

the formatted bytes

Declaration

public static int[] fromBase16toIntArray(String bytes) 

Method Source Code

//package com.java2s;
/*//from  w  w  w .  ja  va2  s. c om
   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 {
    /**
     * @param bytes the String representing the bytes in hex form
     * @return the formatted bytes
     * @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)) {
            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. fromBase16toByteArray(String bytes)
  5. fromBase26(String number)
  6. fromBase36(String base36)
  7. fromBase36(String base36Number)
  8. fromBase58(String input)