如何将 int 转换为十六进制 (uint8) 并将其拆分为 2 个字节
在 C++ 中,我试图将 int 转换为 uint8(或 char)十六进制值。然后将它们分成 2 个字节。 (我正在将 sysex 数据发送到合成器)
例如,1 的 int 应该返回 2 个变量:0x0 和 0x1 一个 int 120(十六进制 78)应该返回 0x7 和 0x8
我一直在尝试使用 stringstream 和 atoi 但似乎无法让它工作..
好吧抱歉,澄清(昨天不得不开车 1000 公里并匆忙发布)我正在编写的代码是将 sysex 通过 midi 发送到合成器。
我有以下问题:我收到 2 个双打(来自 2 个滑块)
双打 a、b;
double a需要转换为hex(uint8),double b需要转换为hex,然后拆分为2个uint8变量。
然后我需要将它们放入 uint8 数组中,如下所示:
const uint8 rawSysEx[13] = {0x65, 0x16, 0x0, 0x0, 0x0, converteda, convertedb1, convertedb2, 0x2, 0x4, 0x4, 0x5, 0x4};
希望这能澄清。
*编辑
int noteNumber = currentMessage.getNoteNumber();
noteNumber = trunc((noteNumber*2.125*10.0)/10.0);
hexNoteNumber = static_cast<char>(noteNumber);
解决方案:我了解到有符号字符的值从 -127 到 + 127,无符号字符的值从 0 到 255,所以这是有效的代码:谢谢大家的输入
const auto ucx {static_cast
(noteNumber)}; char split1 = ucx & 0xf; char split2 = ucx >>> 4u;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论