"开箱"用C

编程入门 行业动态 更新时间:2024-10-25 21:16:35
本文介绍了"开箱"用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的工作从蟒蛇改写脚本为 C 。我是比较新的温度。

I am working on rewriting a script from python to C. I'm relatively new to C.

我在Python中的变量,它包含此值:

I have a variable in PYTHON which contains this values:

x = [chr(113),chr(80),chr(191),chr(70)] y = "".join(x)

这将返回Y的这个值:

y = qP¿F #this is string

现在我要做的就是解开这个变量,其存储到变量的以Z 的得到我想要的结果。像这样的:

Now what I do is unpack this variable, store it to variable z to get the results that I wanted. Like this:

z = struct.unpack("<f",y) print z[0] #unpack returns a tuple of size 1

这是我得到的值是:

x = 24488.2207

这对我而言是正确的。

which for my case is correct.

我在想,如果有一个同样功能的 C ,我可以用这个?

I was wondering if there is a same function in C that I can use for this?

推荐答案

有不需要这样的功能;提供的字节顺序已经是正确的编译器可以处理这种情况本身既可以通过指针铸造或通过联盟键入

There is no need for such a function; provided the endianness is already correct the compiler can handle that case itself either through pointer casting or through a union type.

uint8_t data[4] = {113, 80, 191, 70}; printf("%f\n", (double)(*(float*)data));

...

$ ./a.out 24488.220703

更多推荐

&QUOT;开箱&QUOT;用C

本文发布于:2023-05-25 10:53:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QUOT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!