在 C 中将 Int32 类型转换为 sCplx16 IQ 样本

发布于 2024-10-03 06:20:51 字数 721 浏览 6 评论 0原文

我有以下问题

我有一个结构

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

翻身的咸鱼 2024-10-10 06:20:51

我不确定编译器如何处理带有值传递的函数的类型转换。
但如果你改变函数来传递 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.

若相惜即相离 2024-10-10 06:20:51

您不能依赖结构内部没有填充,这样的字段“别名”仅对第一个字段是安全的。研究是否可以告诉您的编译器不要填充结构(该属性有时称为“打包”),并添加代码来验证 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.

屋檐 2024-10-10 06:20:51

类型转换需要关注的两点是:

  • 字节顺序。如果将代码移植到具有不同字节顺序的机器,则打包数据将不再正确映射到结构字段。
  • 尺寸差异。如果 sizeof(sCplx16) != sizeof(Int32),则无法确定映射结果如何。出现这种差异的原因是 Int16 不完全是 16 位宽,或者可能是结构成员之间填充的结果(尽管如果它们具有相同的类型,则不太可能发生这种情况。只有故意困难的编译器将添加此类不需要的填充)

获得完全可移植代码的最简单方法是编写一个小函数,将打包表示转换为结构:

sCplx16 unpack(Int32 data)
{
  sCplx16 result = { (data >> 16) & 0xFFFF, data & 0xFFFF };
  return result;
}

然后您可以将函数调用为 Fun(unpack(Data));

The two points of concern with the typecasting are:

  • Endianness. If you port the code to a machine with different endianness, then the packed data will no longer correctly map to the structure fields.
  • Size differences. If sizeof(sCplx16) != sizeof(Int32), then there is no telling how the mapping will turn out. This difference could occur because Int16 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:

sCplx16 unpack(Int32 data)
{
  sCplx16 result = { (data >> 16) & 0xFFFF, data & 0xFFFF };
  return result;
}

Then you can call your function as Fun(unpack(Data));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文