How to convert uint16* to uint8*

I have really basic question. I have a function with the following signature:

setValue(uint8_t* keys, size_t size)

and I have a variable of type uint16_t* that I need to convert into a uint8_t* so that I can call the function properly.
How would I do that?

You could cast it:

setValue((uint8_t*) ptr16, s);

That will stop the compiler from complaining. But, depending on what the function does, you may or may not get the expected results.

Thanks. Maybe my problem is more complex than I thought.

I used to have this code which worked fine:

typedef uint8_t MediaKeyReport[2];
// ...

void BleKeyboard::sendReport(uint8_t* mediaKeyReport)
{
    this->inputMediaKeys->setValue((uint8_t*)mediaKeyReport, sizeof(MediaKeyReport));
    this->inputMediaKeys->notify();
}

MediaKeyReport mediaKeyReport;
mediaKeyReport[0] = 8; // Play/Pause
mediaKeyReport[1] = 0;
bleKeyboard.sendReport(&mediaKeyReport);

Then I added a bunch of uint16_t constants that I would like to use without changing them. So I changed my function to accept a MediaKeyReport* (aka uint16_t*) instead of a uint8_t*, but now I have to convert it inside of the function in a way that the ...->setValue() method gets the same uint8_t* array as before. But I just can't get it to work:

const uint16_t KEY_MEDIA_NEXT_TRACK = 1;
const uint16_t KEY_MEDIA_PREVIOUS_TRACK = 2;
const uint16_t KEY_MEDIA_STOP = 4;
const uint16_t KEY_MEDIA_PLAY_PAUSE = 8;
const uint16_t KEY_MEDIA_MUTE = 16;
const uint16_t KEY_MEDIA_VOLUME_UP = 32;
const uint16_t KEY_MEDIA_VOLUME_DOWN = 64;
const uint16_t KEY_MEDIA_LOCAL_MACHINE_BROWSER = 128; /* Opens "My Computer" on Windows */
const uint16_t KEY_MEDIA_CALCULATOR = 256;
const uint16_t KEY_MEDIA_WWW_BOOKMARKS = 512;
const uint16_t KEY_MEDIA_WWW_SEARCH = 1024;
const uint16_t KEY_MEDIA_WWW_STOP = 2048;
const uint16_t KEY_MEDIA_WWW_BACK = 4096;
const uint16_t KEY_MEDIA_CONSUMER_CONTROL_CONFIGURATION = 8192; /* Media Selection */
const uint16_t KEY_MEDIA_EMAIL_READER = 16384;

typedef uint16_t MediaKeyReport;

// ...

void BleKeyboard::sendReport(MediaKeyReport* mediaKeyReport)
{
    this->inputMediaKeys->setValue((uint8_t*)mediaKeyReport, sizeof(MediaKeyReport));
    this->inputMediaKeys->notify();
}


MediaKeyReport mediaKeyReport = KEY_MEDIA_PLAY_PAUSE;
bleKeyboard.sendReport(&mediaKeyReport);

It compiles fine, but it just not working anymore, no errors.

Any ideas how I could get the conversion right?

Don't you think it would be helpful to post the code for setValue()? ALL you code for that matter?

Also, turn up your compiler warning options. Of course, I can't tell for sure because you didn't post your code. But, it looks like you'll be trying to pass a (const uint16_t *) to a function that expects a (uint16_t *). You don't want to do that.

, sizeof(MediaKeyReport))size of a pointer is always constant, regardless of what it points to.

Is that your problem?

Honestly, I don't think the code of setValue matters. As I said, when it's called correctly it works just fine. I am not sure where exactly the method comes from it's quite complex and part of a library that I use. You can check out the old code that works here: ESP32-BLE-Keyboard/BleKeyboard.cpp at master · T-vK/ESP32-BLE-Keyboard · GitHub

I have everything set to be as verbose as the IDE allows. I don't get a single warning.

size of a pointer is always constant, regardless of what it points to.

Yes, I could have written 2 instead of sizeof(MediaKeyReport), but it doesn't make a difference, the size is always two because I'm always passing two bytes. Or maybe the correct way of saying it is that the variable the pointer is pointing to is always 2 bytes.

felic:
Honestly, I don't think the code of setValue matters. As I said, when it's called correctly it works just fine.

Well, I did think it mattered. But, it's your code so you know best. Good Luck.

It comes from a library that is not written by me. In that regard it is not "my code".

Here you go, I think I found it:

/**
 * @brief Set the value of the descriptor.
 * @param [in] data The data to set for the descriptor.
 * @param [in] length The length of the data in bytes.
 */
void BLEDescriptor::setValue(uint8_t* data, size_t length) {
	if (length > ESP_GATT_MAX_ATTR_LEN) {
		ESP_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, ESP_GATT_MAX_ATTR_LEN);
		return;
	}
	m_value.attr_len = length;
	memcpy(m_value.attr_value, data, length);
}

source