Convert byte array to int by index - Android File Input Output

Android examples for File Input Output:Byte Array Convert

Description

Convert byte array to int by index

Demo Code


//package com.java2s;

public class Main {
    public static int[] byte2int(byte[] bytes, int index, int size) {
        int[] temp = new int[size * 2];
        int k = 0;
        for (int i = 0; i < size; i++) {
            temp[k++] = 0xF0 & bytes[index + i];
            temp[k++] = 0xF0 & (0x0F & bytes[index + i]) << 4;
        }//from  w w w .jav a2  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 String toHex(byte b) {
        return ("" + "0123456789ABCDEF".charAt(0xf & b >> 4) + "0123456789ABCDEF"
                .charAt(b & 0xf));
    }
}

Related Tutorials