Convert hex string array to int array - Android java.lang

Android examples for java.lang:array convert

Description

Convert hex string array to int array

Demo Code


//package com.java2s;

public class Main {
    public static int[] str2int(String[] strings) {
        int[] temp = new int[strings.length * 2];
        int k = 0;
        for (int i = 0; i < strings.length; i++) {
            temp[k++] = hex2int(strings[i].charAt(0));
            temp[k++] = hex2int(strings[i].charAt(1));
        }/*  ww  w .ja va  2  s  . c o m*/
        return temp;
    }

    public static String toString(byte[] bytes, int size) {
        String ss = "";
        for (int i = 0; i < size; i++) {
            ss += toHex(bytes[i]);
            //+" ";
        }
        return ss;
    }

    public static int hex2int(char c) {
        if (c >= '0' && c <= '9') {
            return (c - '0') * 16;
        }
        if (c >= 'A' && c <= 'F') {
            return (c - 'A' + 10) * 16;
        }
        return 0;
    }

    public static String toHex(byte b) {
        return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF"
                .charAt(b & 0xf));
    }
}

Related Tutorials