如何将结构转换为 char 指针?
实际上,我想创建一个 spi 接口,在其中我能够分别更改位 18-22 和位 1-16(我想要位 1-16 上有一个单热地址,而位 18-22 上有一个二进制编码的十进制)这里是我打算如何实现该结构
struct spi_out
{
unsigned int BCDADDR : 4
unsigned int OHADDR : 16
/// Some other spi bit addresses making up the rest of the 3 bytes
所以这是我的问题
我希望能够访问 BCD 地址并直接对其进行编码,例如: spi_out.bcd = 5// 以便访问第 6 个单元格
,但我也想使用运算符函数按照我需要的方式格式化这些位,因为我需要按照放入变量的顺序排列变量,而且我无法找出执行此操作的简单方法,因为我不想将 LUT 放入运算符函数中,但我需要能够将位字符串转换为 char 指针,以便可以将函数中的 3 个字节信息提供给硬件抽象函数HAL_SPI_Transmit()
,就像我知道数据保留为 3 个字节一样,所以我不明白为什么我不能这样访问它们>:/
Effectively I want to make an spi interface where I'll be able to change bits 18-22 and bits 1-16 separately (I want a one hot address on bits 1-16 and a binary coded decimal on bits 18-22) here's how I intend on implementing the struct
struct spi_out
{
unsigned int BCDADDR : 4
unsigned int OHADDR : 16
/// Some other spi bit addresses making up the rest of the 3 bytes
So here's my problem
I want to be able to access BCD address and encode it directly eg: spi_out.bcd = 5// in order to access the 6th cell
but I also want to use the operator function to format the bits the way I need them since I need the variables in the order I put them in and I can't figure out a simple way of doing this since I wouldn't want to have to put an LUT inside an operator function but I need to be able to cast the string of bits to a char pointer so the 3 bytes of information from the function can be fed to a hardware abstraction functionHAL_SPI_Transmit()
, like I know the data is kept as 3 bytes so I don't see why I can't access them as such>:/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我开始意识到我的问题是用一种令人讨厌的令人困惑的方式措辞的,但我实际上找到了我自己问题的答案,那就是使用 Union 关键字,这意味着我可以定义一个联合,这意味着我可以创建一种可以被视为访问各个位或 4 个字符数组的结构体的类型。我没有意识到这个存在,但这里有一个堆栈溢出问题的链接,我在其中找到了答案
访问结构中的位字段
对不起大家
Okay so i have come to appreciate that my question was worded in such an annoying confusing way but i have actually found an answer to my own question, that is to user the Union keyword, this means i can define a union that means i can create a type that can be treated as a struct to access the individual bits or an array of 4 chars. I did not realize this existed but here is a link to the stack overflow question where i found my answer
accessing bit fields in structure
sorry guys