In c++ i'm trying to convert to an int to uint8 (or char) hex value. Then spitting them into 2 bytes. (I'm sending sysex data to a synth)
For example an int of 1 should return 2 variables: 0x0 and 0x1 an int of 120 (hex 78) should return 0x7 and 0x8
I've been trying with stringstream and atoi but can't seem to get it to work..
Ok Sorry, clarififation (had to drive 1000km yesterday and posted in a hurry) The code I'm working on is to send sysex over midi to a synth.
I have following problem: I receive 2 doubles (from 2 sliders)
double a,b;
Double a needs to be converted to hex (uint8), double b needs to be converted to hex, and then split into 2 uint8 variables.
Then i need to put them in an array of uint8's like this:
const uint8 rawSysEx[13] = {0x65, 0x16, 0x0, 0x0, 0x0, converteda, convertedb1, convertedb2, 0x2, 0x4, 0x4, 0x5, 0x4};
Hope this clarifies.
*edit
int noteNumber = currentMessage.getNoteNumber();
noteNumber = trunc((noteNumber*2.125*10.0)/10.0);
hexNoteNumber = static_cast<char>(noteNumber);
Solution: I learned that a signed char has values from -127 to + 127 and that unsigned char has values from 0 to 255 so this is the code that works: thank you all for the input
const auto ucx {static_cast<unsigned char>(noteNumber)}; char split1 = ucx & 0xf; char split2 = ucx >> 4u;