十六进制和二进制运算
我有问题要解决,但不知道该怎么做。我的程序从串口接收十六进制值的字符串(如 DFF7DF)。我需要将其转换为二进制形式,丢弃前四位,将第五位作为符号位,将接下来的 12 位作为值。
我需要获取正常 INT 的值。
我能够在 MATLAB 中编写这样的程序,但我需要 C++ 才能在我的 Linux Arm 板上运行它。
预先感谢您的帮助! 马尔钦
I have problem to solve and have no idea how to do that. My program receives from serial port string with hex value (like DFF7DF). I need to convert it to binary form, discard first four bits, take fifth bit as sign bit and next 12 bits as a value.
I need to get value as normal INT.
I was able to make such program in MATLAB, but I need C++ to be able to run it on my linux arm board.
Thanks in advance for help!
Marcin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你可以这样做:
You could do something like:
正确的答案取决于一些约定 - 十六进制字符串是大端还是小端?您是从最高有效位开始计数还是从最低有效位开始计数?总是有 6 个十六进制字符(24 位)吗?
无论如何,这是一种大尾数法的解决方案,始终为 24 位,从最高有效位开始计数。我相信如果我的某些假设是错误的,您将能够调整它。
The correct answer depends on a few conventions - is the hex string big-endian or little-endian? Do you start counting bits from the most significant or the least significat bit? Will there always be exactly 6 hex characters (24 bits)?
Anyways, here's one solution for a big-endian, always-24-bits, counting from most significant bit. I'm sure you'll be able to adapt it if some of my assumptions are wrong.
问题被标记为 C++,但每个人都在使用 C 字符串。以下是如何使用 C++ STL 字符串执行此操作
(您的问题中的第二个“位摆弄”部分不清楚。如果您澄清的话,我将更新答案。)
The question is tagged C++ but everyone is using C strings. Here's how to do it with a C++ STL string
(The second "bit-fiddling" part isn't clear from your question. I'll update the answer if you clarify it.)
您必须检查机器类型的字节顺序,但这基本上就是这个想法。
确保对
无符号
类型执行移位运算符。You have to check your machine type for endian-ness but this is basically the idea.
Make sure you perform your bit-shift operators on
unsigned
types.这是一个需要遵循的模式:
其余的只是位移位。
Here's a pattern to follow:
The rest is just bit-shifting.