Insert values from an array of bytes into an int array without converting them

1

I have this int vector:

int[] vetor = new int [dataRegCodeToCompare.length];

and this vector byte: (which receives the digest of another byte array)

byte[] dataRegCodeToCompare = md5.digest(toHash);

I want to put the values of the byte array dataRegCodeToCompare in the array int vetor . But, without converting the values; for example in dataRegCodeToCompare[0]=12 so I want vetor[0]=dataRegCodeToCompare[0] that vetor[0]=12 . This is with all values of array .

I can not just make vetor = dataRegCodeToCompare .

    
asked by anonymous 22.04.2015 / 20:40

1 answer

2

As far as I know, there is no Java ready function that allows you to do this in a row, perhaps because it is relatively simple.

Just in case, I've confirmed what Jon Skeet talked about it , and at least not in 2011 existed.

You can implement this yourself anyway:

public class ByteInt {
    public static void main(String[] args) {
        byte[] bytes = new byte[]{1, 2, 3, 4, (byte) 130}; //exemplo
        int[] sin = converteByteSinalizadoParaInt(bytes);
        int[] naoSin = converteByteNaoSinalizadoParaInt(bytes);

        System.out.println("Sinalizados");
        for(int s: sin) {
            System.out.println(s);
        }
        System.out.println("\nNão Sinalizados");
        for(int ns: naoSin) {
            System.out.println(ns);
        }
    }

    public static int[] converteByteSinalizadoParaInt(byte[] entrada) {
        int[] sin = new int[entrada.length];
        for(int i =0; i<entrada.length; i++) {
            sin[i] = entrada[i];                
        }
        return sin;
    }

    public static int[] converteByteNaoSinalizadoParaInt(byte[] entrada) {
        int[] naoSin = new int[entrada.length];
        for(int i =0; i<entrada.length; i++) {
            naoSin[i] = entrada[i] & 0xff;              
        }
        return naoSin;
    }
}

Output:

  

Signed
  1
  2
  3   4
  -126

     

Not Signed
  1
  2
  3   4
  130

    
22.04.2015 / 21:18