0

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;
    
  • If you just want to split a value into two hex digit values: `uint8 in; /* input in */ uint8 out1 = in & 0xf, out2 = in >> 4u;` – Scheff's Cat Mar 15 '22 at 06:39
  • 1
    What have you tried? What problem did you encounter? Please show a [mre] – Alan Birtles Mar 15 '22 at 06:44
  • Ok i tried this but to no avail: std::stringstream sstreamkitDialValue; sstreamkitDialValue << std::hex << kitDialValue - 1; std::string kitDialValueHex = sstreamkitDialValue.str(); kitNoteto8 = atoi (kitDialValueHex.substr(1, 3).c_str ()); – user1359023 Mar 15 '22 at 07:03
  • 2
    There is no such thing as a 'hex value'. Hex is a representation, not a value type. – user207421 Mar 15 '22 at 07:06
  • You should [edit] clarifications into your question, code in comments doesn't really work – Alan Birtles Mar 15 '22 at 07:20
  • @user207421 It may be out of OP bounds but, C++ precedes a hexadecimal value that it prints with the characters "0x" to make it clear that the value is in base 16. Is hexadecimal value here written accidentally? –  Mar 15 '22 at 07:23
  • @OrkhanAliyev -- by default the C++ standard streams do not show the base prefix. You can turn the prefix on with the manipulator `std::showbase` and off with `std::noshowbase`. – Pete Becker Mar 15 '22 at 14:33
  • @OrkhanAliyev Of course, but it's irrelevant. That's a convention used by the compiler. The OP is trying to do something or other at runtime. – user207421 Mar 16 '22 at 08:59
  • @Scheff'sCat you where right! Works perfectly, thank you so much! I've been banned apprantly to post anwsers so don't know how to close this thread. Thank you all. – user1359023 Mar 17 '22 at 11:34
  • @Scheff'sCat ok, so not just yet :D the conversion goes well for ints upto 127 beyond that I get weird values, any ideas? – user1359023 Mar 17 '22 at 15:07
  • Cannot confirm. It works for the full range of `uint8_t`: [Demo on coliru](http://coliru.stacked-crooked.com/a/4df814546d2a52a1). There must be something else. Please, make a [mcve]. There are things in your question which make me uncertain about your intention. (I admit I haven't any experience with sysex, and google didn't help much.) My original comment just addressed the original question - to split one 8 bit unsigned into two "hex digit values"... – Scheff's Cat Mar 17 '22 at 15:49
  • What is the range of values you receive from the sliders in `double a, b`? Considering the fact that `double`s store floating point values in 8 bytes, and you intend to convert into 1 unsigned byte and 2 unsigned bytes, there is much room for speculation... (I mean, there are multiple ways of conversion imaginable but most (all?) of them come with some kind of loss.) – Scheff's Cat Mar 17 '22 at 15:58
  • @Scheff'sCat see edit above I actually take in the value of the slider into an int because the value doesn't have decimals. So it will always be between 0 and 255, then i need to spilt that value in 2 variables of 0 to 127 each – user1359023 Mar 17 '22 at 17:05

0 Answers0