C# 3 字节整数
我正在开发一个项目,需要在字节级别处理整数。 由于节省空间是首要考虑因素,因此我只需要非常小的(并且可变长度的整数)。
有没有办法可以将 int '4096' 转换为 3 个字节? 或者'1053'变成2个字节?
显然我可以手动执行= (byte[0] * 256) + (byte[1]),但我想知道是否有一个更简单的选项可以将 int 转换为 x 字节并再次转换回来?
I am working on a project where I need to deal at the byte level with integers. Since saving space is a primary consideration, I only need very small (and variable length ints).
Is there a way that I can turn the int '4096' into 3 bytes? or '1053' into a 2 bytes?
Obviously I cna do it manually = (byte[0] * 256) + (byte[1]), but i was wondering if there is an easier option to convert the int into x bytes and back again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
BitConverter.GetBytes
将为您获取字节。BitConverter.ToInt32 将从字节中获取 32 位 int。
BitConverter.GetBytes
will get you the bytes.and
BitConverter.ToInt32
will get you a 32 bit int from the bytes.你必须做一些位移。 如果你使用十六进制,那就容易多了,因为每个数字(我的意思是每个数字,但数字是以 10 为基数的,hexgit)代表四位。
You have to do some bit shifting. It is much easir if you work with HEX, since every number(I mean every digit, but digit is for base 10, hexgit) represent four bits.
您可以进行可变长度整数编码。 多年前的旧方法是使用每个字节的高位来表示该整数继续到另一个字节。 因此,每个字节会丢失一位,但会获得小整数。 这在每个最后一个字节都很重要的持久存储中最有用。
示例:假设我们正在处理无符号整数,我们会让
0-127 占用 1 个字节,128-16383 占用 2 个字节,等等...
有关更复杂的方法,请查看 此页面
You could do variable length integer encoding. The old method from years ago was to use the high bit of each byte to denote that the integer continues to another byte. So you lose one bit per byte, but gain small integers. This is mostly useful in persistent storage where every last byte counts.
Example: Assume we are dealing with unsigned integers, we would have
so 0-127 take 1 byte and 128-16383 takes 2 bytes, etc...
For more complex ways of doing this check out this page
只是为了增加疯狂性,让我们使用旧的 C 风格联合技巧在 C# 中进行操作:
然后,当您想要“转换”时,执行以下操作:
但这实际上仅在您希望“节省”成本时才有帮助从一个字中移出一个字节。 我没有查看生成的 SMIL,所以我不知道这是否比任何其他解决方案更便宜。
Just for added insanity, let's do it in C# using the old C-style union trick:
And then, when you want to "convert", do this:
But that really only helps if you're looking to "save" the cost of bit-shifting out a single byte from a word. I haven't looked at the generated SMIL, so I don't know whether this is any cheaper in comparison to any other solution.
你可以吗? 当然。 会节省空间吗? 也许吧,取决于你想做多少工作。 您必须了解处理器是 32 位的,这意味着它有 4 字节寄存器,因此这就是它存储和访问内容的方式。 要强制使用 3 字节“int”,您必须将其保存在字节数组中,并在使用前将其从数组中提取到对齐的地址。 这意味着,如果您将其存储得很短,编译器要么会将其填充(并且您将失去您认为已经创建的任何效率),要么读写速度会慢。
如果这是一个桌面应用程序,那么节省空间究竟如何成为首要考虑因素,尤其是在每个元素使用 1 个字节时? 元素访问的性能损失可能会改变您对一个字节的重要性的看法。
我认为,如果 1 个字节确实很重要,那么也许,只是也许,你无论如何都使用了错误的语言。 如果您首先不安装和使用 CLR,那么您将节省的字节数是这些字节的很多。
旁注:您还需要进行移位,而不是乘法(尽管编译器可能会为您实现)。
Can you? Sure. Will it save any space? Maybe, depending on how much work you want to do. You have to understand that the processor is 32-bit, meaning it has 4 byte registers, so that's how it's going to want to store and access things. To force a 3-byte "int" you'll have to keep it in a byte array, and extract it from the array to an aligned address before use. That means that if you store it short, the compiler will either pad it out (and you'll lose any efficiency you think you've created) or it will be a lot slower to read and write.
If this is a desktop app, how exactly is saving space a primary consideration, especially when talking 1 byte per element? The perf penalty for element access may change you mind about how critical that one byte is.
I'd argue that if that 1 byte truly is important, that maybe, just maybe, you're using the wrong language anyway. The number of bytes you'd save my not installing and using the CLR in the first place is a lot of those bytes.
Side note: You'd also do a shift, not a multiplication (though the compiler would likely get there for you).