c#如何将long转换为单字节
我正在尝试将 ac# 函数重写为 python。现在我看到这样一行:
timeArray[i - 1] = (byte)timestamp;
显然它只是解析 var“timestamp”,它是一个长度为 10 的长整型字节。但这是如何工作的,以及如何在 python 中重现它? 作为参考,这里是 C# 代码的完整版本:
var timestamp = long.Parse("1647079653");
var timeArray = new byte[8];
timestamp /= 30L;
for (var i = 8; i > 0; i--)
{
timeArray[i - 1] = (byte)timestamp;
timestamp >>= 8;
}
这返回一个 [0,0,0,0,3,69,191,127] 的数组,
当我在 python 中执行此操作时(使用内置的字节函数),我得到了疯狂的数字数组的每个索引,尽管时间戳的初始值是正确的(如 6-7 位数字) 有什么想法吗?
Im trying to rewrite a c# function to python. Now ive come to a line which looks like this:
timeArray[i - 1] = (byte)timestamp;
Obviously it just parses the var "timestamp", which is a long of length 10 to a byte. But how does this work, and how can i reproduce it in python?
for reference, here the full version of the c# code:
var timestamp = long.Parse("1647079653");
var timeArray = new byte[8];
timestamp /= 30L;
for (var i = 8; i > 0; i--)
{
timeArray[i - 1] = (byte)timestamp;
timestamp >>= 8;
}
this returns an array of [0,0,0,0,3,69,191,127]
when i do it in python (using the built in bytes-function) i get insane numbers for each index of the array, altough the inital values of the timestamp are correct (like 6-7 digits)
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我想通了。它只取相应二进制的最后 8 位,这就是字节。工作的Python代码:
Okay i figured it out. It just takes the last 8 bits of the corresponding binary and this is then the byte. The working python code: