问题描述
我的工作从蟒蛇改写脚本为 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!