Convert int to byte swift 2.3

2

I have an int array I want to pass it to byte array, how could I do it, either for swift 2.3 or objetive-c

let constant : [Int] = [0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xC0, 0x00, 0x00, 0x00, 0x00];

I want to turn it into

let constant2 : [UInt8] = [.......]

It does not matter if it's all one hit, or position by position, that is, take the value of 0xC0 and pass it to byte and put it in the position of the array. But I do not know how.

an example in java would have this result

//[Int] swift es un array de enteros
[192, 192, 192, 192, 0, 0, 0, 0, 192, 192, 192, 192, 0, 0, 0, 0]
//[UInt] swift es un array de bytes solo que en java es diferente pero algo asi quiero hacer.
[-64, -64, -64, -64, 0, 0, 0, 0, -64, -64, -64, -64, 0, 0, 0, 0]
    
asked by Sergio Aldo Mejía Rodríguez 22.12.2016 в 01:04
source

1 answer

0

Good morning, in Objective-C you can do the following:

int len = sizeof(int);
for (int val in array) {
    char* bytes = (char*) &val;

    //Agregas al nuevo array 
}

First you get the size of int to know how many char are and then you convert each value of the array and add it to the new array of chars .

    
answered by 27.01.2017 в 19:57