将 C 字节数组转换为 long long
我的应用程序中有一个包含以下数据的 8 字节数组:
00000133339e36a2
该数据代表一个 long (在数据写入的平台上,在 Mac 上这将是一个 long long),其值为
1319420966562
在实际应用程序中,这是一个半随机数据集,因此数字总是不同的。因此,我需要将字节数组转换为可打印的long long。
我尝试过将数据直接转换为 long long,但我想出了
1305392
我应该在哪里看到上面的数字。
对于那些比我更有 C 字节操作经验的人来说,如何正确地将字节数组转换为 long long?
编辑:奇怪的是,您的所有解决方案都保持输出相同的数字:866006690。这是数据的最后四个字节的十进制等效值。
I have an 8-byte array in my application with this data:
00000133339e36a2
This data represents a long (on the platform the data was written in, on a Mac this would be a long long) with the value of
1319420966562
In the actual application this is a semi-randomized set of data, so the number will always be different. Therefore, I need to convert the byte array into a printable long long.
I've tried casting the data directly into a long long, but I came up with
1305392
where I should have been seeing the above number.
For those of you with more experience in C byte manipulation than I do, how would I correctly convert a byte array to a long long?
EDIT: Strangely, all of your solutions keep outputting the same number: 866006690. That is the decimal equivalent of the last four bytes of the data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这似乎是联合有用的(罕见)情况之一:
This seems one of the (rare) situations where an union is useful:
复制(或仅使用)
le_long_long_array.byte[]
作为数组;回读
le_long_long_array.longlong
例如:
Fuller 示例:
产生:
正如您对小端数据的期望。
Copy (or just use)
le_long_long_array.byte[]
for the array;read back
le_long_long_array.longlong
For example:
Fuller example:
Produces:
as you would expect for little endian data.
假设它们以大端存储。
Assuming they're stored big-endian.
根据您想要的字节顺序,类似这样的事情会起作用:
Depending on your desired endianness, something like this would work:
好问题!和很好的答案!顺便说一句:
ll2
现在等于ll1
。我必须管理,但是,我更喜欢
union
示例。 :)Great question! And great answers! To throw in a cent:
ll2
is now equal toll1
.I must admin, however, I like the
union
examples better. :)也许是指针符号?
或者如果可能的话
Perhaps a pointer notation?
Or if possible