位移位和数据解释
好的,我想解释一下位移位如何工作以及如何从字节数组构建数据。语言并不重要(如果需要示例,我知道 C、C++、Java 和 C#,它们都遵循相同的移位语法,不是吗?)
问题是,如何从 byte[] 转换为一堆字节在一起的东西? (无论是 16 位整数、32 位整数、64 位整数、n 位整数)更重要的是,为什么?我想了解并学习如何自己制作,而不是从互联网上复制。 我知道字节序,我主要搞乱了小字节序的东西,但是解释一下两个系统的一般规则会很好。 非常非常感谢!! 费德里科
Ok, I'd like to have an explanation on how bit shifting works and how to build data from an array of bytes.The language is not important (if an example is needed, I know C,C++, Java and C#, they all follow the same shifting syntax,no?)
The question is, how do I go from byte[] to something which is a bunch of bytes together? (be it 16 bit ints, 32 bits ints, 64 bit ints, n-bits ints) and more importantly, why? I'd like to understand and learn how to make this myself rather then copy from the internet.
I know of endianess, I mainly mess with little endian stuff, but explaining a general rule for both systems would be nice.
Thank you very very much!!
Federico
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常编译器会为您处理字节顺序。因此,您可以将字节与移位一起进行“或”运算,而不必对此过于担心。
我自己过去曾使用过一些作弊方法来节省周期。当我知道数据按从最低到最高的顺序排列在数组中时,您可以将字节转换为更大的类型并按 walker 的大小遍历数组。例如,此示例一次遍历 4 个字节并在 C 中写入 4 个字节整数。
输出:
1
256
65536
16777216
这可能对 C# 和 Java 没有好处
Usually the compiler is going to take care of endianess for you. So you can OR bytes together with shifting and not worry too much about that.
I've used a bit of a cheat myself in the past to save cycles. When I know the data to be in arrays least to most significant order, then you can cast the bytes into larger types and walk the array by size of walker. For example this sample walks 4 bytes at a time and writes 4 byte ints in C.
Output:
1
256
65536
16777216
This is probably no good for C# and Java
您需要考虑数据的字节顺序。从那里,它取决于数据类型 - 但这里是一个例子。假设您想要从 4 字节数组转换为无符号 32 位整数。我们还假设字节的顺序与它们开始时的顺序相同(所以我们不需要担心字节顺序)。
您也可以等效地使用 |= 和 OR 字节。另外,如果您在结构上尝试此技巧,请注意您的编译器可能不会连续打包值,尽管通常您可以使用编译指示设置行为。例如,gcc/g++ 在我的架构上打包与 32 位对齐的数据。
对于 C#,BitConverter 应该可以满足您的要求。我知道 Python、PHP 和 Perl 使用一个名为 pack() 的函数来实现此目的,也许 Java 中有一个等效的函数。
You will want to to take into consideration the endianness of the data. From there, it depends on the data type - but here is an example. Let's say you would like to go from an array of 4 bytes to an unsigned 32 bit integer. Let's also assume the bytes are in the same order as they started (so we don't need to worry about the endianness ).
You could equivalently use |= and OR the bytes too. Also, if you attempt this trick on structures be aware that your compiler probably won't pack values in back-to-back, although usually you can set the behavior with a pragma. For example, gcc/g++ packs data aligned to 32 bits on my architecture.
On C#, BitConverter should have you covered. I know Python, PHP, and Perl use a function called pack() for this, perhaps there is an equivalent in Java.
对于位移位我会说。字节1 << n 会将 byte1 的位左移 n 次,结果可以通过将 byte1 乘以 2^n 得到...对于 >>n 我们必须将 byte1 除以 2^n。
for bit shifting i would say. byte1 << n will shift bits of byte1 to n times left and result can be get by multiplying byte1 with 2^n...and for >>n we have to divide byte1 by 2^n.
嗯...这取决于你在寻找什么。实际上没有办法转换为 n 位整数类型(无论如何它总是一个字节数组),但您可以将字节转换为其他类型。
Hm... it depends what you're looking for. There's really no way to convert to an n-bit integer type (it's always an array of bytes anyways) but you can shift bytes into other types.