How to translate data from c language void * pointer to [UInt8 ] in swift code

35 views Asked by At

A swift function name as getDataFromC which can connect with C function. And a C function name as ExternalMethod which will receive the connect from getDataFromC. The parameter will be fill by call setData function.

I can confirm that the entire structure return to getDataFromC was successful. Because I print the data of other structure members to check the correctness of the data.

However, I don't know how to translate the largeData to [UInt8].

//Bridging header file
struct myStruct
{
    void *largeData;
    int largeDataLength;
    int smallData[3];
}
//C file

void setData(myStruct* data)
{
    int i = 0;
    uint8_t array[1024];
    
    memset(array,0x66,1024);
    
    data->largeData = array;
    data->largeDataLength = 1024;
    data->smalData[0] = 0x99;
    data->smalData[0] = 0x88;
    data->smalData[0] = 0x77;
}

void ExternalMethod(myStruct* data)
{
    setData(&output);
    
}
//Swift file
func test() {

    var output = myStruct()
    getDataFromC(&output);
    var myPointer: UnsafeMutableRawPointer = output.largeData
    /* Here: I don't know how to translate data from myPointer to [UInt8] */
    
    print("smallData: \(output.largeDataLength)") // 1024 transmission is correct
    print("smallData: \(output.smallData.0)")     // 0x99 transmission is correct
    print("smallData: \(output.smallData.1)")     // 0x88 transmission is correct
    print("smallData: \(output.smallData.2)")     // 0x77 transmission is correct
    
}

I try UnsafeMutableRawPointer type. I don't know what to do for the following action. I expected that output.largeData can be translate to [UInt8] type.

0

There are 0 answers