convert double to hex and split that into 2 hex variables

Hey everyone,

I'm new to c++ and I'm trying to convert a double to hex and split it.

For example if the double is 1 it needs to be converted to hex and splitted to 2 char var1 0 and char var2 1
If the double is 10 the hex converted should be A and then splitted into char var1 0 and char var2 A
if the double is 250 the hex converted would be FA and then splitted into char var1 F and char var2 A

I'm programming a midi conversion for a synth so the max notenumber would never go beyond 255

I already figured out how to cast the double to char but i'm complete lost on how to do the split...

1
2
  char hexkit = static_cast<char>(kitDialValue);
Last edited on
> splitted to 2 char var1 0 and char var2 1
What do you mean here?

You can have a single char with a numeric value of 0x01.
This is easy, it's what you're doing at the moment.

Or a string of two chars with the visual hex representation of 01, which would be "01".
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <sstream>
using namespace std;
 
int main(void)
{
    double x = 160.4;
    ostringstream os;
    os << std::hex << int(x);
    std::cout << "Converted=" << os.str() << std::endl;
    return 0;
}

Use os.str().c_str() if you want the C-style char array.
Salem, tx for your answer, I found it in the mean time:

1
2
3
4

char split1 = hexNoteNumber & 0xf;
char split2 = hexNoteNumber >> 4u;


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main() {
	constexpr const char* hex {"0123456789ABCDEF"};

	const double kitDialValue {250.2};

	const auto ucx {static_cast<unsigned char>(kitDialValue)};
	const char var1 {hex[ucx >> 4]};
	const char var2 {hex[ucx & 0xf]};

	std::cout << var1 << var2 << '\n';
}



FA

Hey seeplus,

Your code actually works perfect (my code above only works untill 127) but when i insert the variables into this matrix they come out not in a hex format

 
const char rawSysEx[14] = {0x41, 0x10, 0, 0, 0, 0x45, 0x12, 0x10, hexKit, hexTrack, 4, var1, var2, hexCheckSum};


Any ideas? My code puts them out in hex format but like I said, after 127 something goes wrong.
Post your whole code so that we can see what's going on. If you need to deal with a value between 128 and 255 then you need to cast to unsigned - like I did in my code L8. char is a signed type and has values -128 to 127. unsigned char has values 0 to 255

Ok, so I'm reading a midi note, this an int from 0 to 120, then I do a calculation on it to send it to a synth. I need to convert it from 0/120 to 0/255:

1
2
noteNumber = currentMessage.getNoteNumber();
                       noteNumber = trunc((noteNumber*2.125*10.0)/10.0);


So this value will always be from 0 to 255

Now i need the split magic so i can put the two variables into the array:


const char rawSysEx[14] = {0x41, 0x10, 0, 0, 0, 0x45, 0x12, 0x10, hexKit, hexTrack, 4, var1, var2, hexCheckSum};


every value in this array will indeed be a hex between 0 and 127

wimvandenborre wrote:
so I'm reading a midi note, this an int from 0 to 120

Please don't split/double post. Now there are two topics dealing with promoting a range of numbers to different range.
For the conversion to hex to work, noteNumber needs to be unsigned char. So from my code above:

 
const auto ucx {static_cast<unsigned char>(noteNumber)};

seeplus, indeed that was it, signed char is from -127 to to 127 and unsigned from 0 to 255
Thank you for the solution!!
Topic archived. No new replies allowed.