在 C 中将 Int32 类型转换为 sCplx16 IQ 样本
我有以下问题
我有一个结构
typedef struct
{
Int16 real;
Int16 imag;
}sCplx16;
Int32 Data; //Data has real-imag packed into 32 bits. real-imag are 16 bits each.
现在我将 Int32 值与 real-imag 打包在一起。我已经编写了接受 sCplx16 参数的函数,但我不想将其设置为 Int32,因为函数内部会有大量更改。
为了避免这种情况,我对变量进行了类型转换,
Fun( (sCplx16)Data);
fun.c
Fun(sCplx16 DataPtr)
{
//
}
Inside the function, I find that value received are mapped correctly to
DataPtr.real and DataPtr.imag.
Is this typecasting correct? Someone told me that it will vary with compiler.
meaning imaginary will be first 16 bits and real will be next 16 bits.
我认为只有字节序会影响这个而不是编译器
请分享您的意见
谢谢
I have following problem
I have a structure
typedef struct
{
Int16 real;
Int16 imag;
}sCplx16;
Int32 Data; //Data has real-imag packed into 32 bits. real-imag are 16 bits each.
Now I am getting Int32 Value with real-imag packed together. I have already written function that takes sCplx16 argument and I dont want to make it Int32 as there is going to be loads of changes inside the function.
To avoid this, I typecasted the variable
Fun( (sCplx16)Data);
fun.c
Fun(sCplx16 DataPtr)
{
//
}
Inside the function, I find that value received are mapped correctly to
DataPtr.real and DataPtr.imag.
Is this typecasting correct? Someone told me that it will vary with compiler.
meaning imaginary will be first 16 bits and real will be next 16 bits.
I think only endianness affects this and not compiler
Please share you opinion
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定编译器如何处理带有值传递的函数的类型转换。
但如果你改变函数来传递 int32 的指针,那么据我所知它只会受到字节顺序的影响。
I'm not sure about how compiler dealing with typecasting for the function with value pass.
But if you change the function to pass a pointer of the int32, then so far as I know it would only be affected by the endian order.
您不能依赖结构内部没有填充,这样的字段“别名”仅对第一个字段是安全的。研究是否可以告诉您的编译器不要填充结构(该属性有时称为“打包”),并添加代码来验证
sizeof (sCplx16)
是否符合您的预期为,即32 / CHAR_BIT
。You can't rely on there being no padding inside the structure, "aliasing" of fields like that is only safe for the first field. Investigate if it's possible to tell your compiler(s) to not pad the structure (the attribute is sometimes called "packed"), and also add code that verifies that
sizeof (sCplx16)
is what you expect it to be, i.e.32 / CHAR_BIT
.类型转换需要关注的两点是:
sizeof(sCplx16) != sizeof(Int32)
,则无法确定映射结果如何。出现这种差异的原因是Int16
不完全是 16 位宽,或者可能是结构成员之间填充的结果(尽管如果它们具有相同的类型,则不太可能发生这种情况。只有故意困难的编译器将添加此类不需要的填充)获得完全可移植代码的最简单方法是编写一个小函数,将打包表示转换为结构:
然后您可以将函数调用为
Fun(unpack(Data));
The two points of concern with the typecasting are:
sizeof(sCplx16) != sizeof(Int32)
, then there is no telling how the mapping will turn out. This difference could occur becauseInt16
is not exactly 16 bits wide, or possibly as a result of padding between the structure members (although that is unlikely if they have the same type. Only a compiler that is willfully difficult will add such unneeded padding)The easiest way to get fully portable code is to write a small function that converts the packed representation into the structure:
Then you can call your function as
Fun(unpack(Data));