如何将位转换为字节?
如果我有一个 x 位值,用 1 和 0 分别表示 true 和 false。我如何将这些转换为 8 位字节?我不想知道如何将位数转换为字节数(x/8)。我想知道如何将这样的内容转换:
10000010 到字节
或
100000101000001 到浮点数
If I have a x-bit value represented as 1s and 0s representing true and false respectively. How would I convert these to 8-bit bytes? I don't want to know how to convert the number of bits to the number of bytes(x/8). I want to know how to convert something like this:
10000010 to a byte
or
100000101000001 to a float
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅:
如何将位转换为字节?
refer to:
How can I convert bits to bytes?
这个问题没有实际意义(没有更多信息)。位可以以多个顺序(字节序)表示,低位位在前或在后。您需要有一个定义的字节顺序。一旦从单字节转移到字,字节序问题就变得更具挑战性,因为位的顺序可能与字节的顺序不同。
从整数转向浮点数又是另一层复杂性。浮点数具有与位顺序完全不同的不同表示形式。
可移植代码将使用定义的表示(网络顺序、printf 等),并且远离尝试手动打包位。
The question is moot (without more information). Bits may be represented in multiple orders (endians), with the low order bit coming first or last. You need to have a defined endian order. Once you move from single bytes to words, the endian question becomes even more challenging since the order of bits may be different from the order of the bytes.
Moving from integers to floating point numbers is yet another level of complexity. Floating point numbers have different representations which are entirely different from the order of bits.
Portable code will use defined representations (network order, printf, etc) and stay far away from trying to hand-pack bits.
所以我猜你想要
1 转换为 1,
10 转换为 2,
11 转换为 3,
ETC。,
1001011 转换为 1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 + 0*2^4+ 0*2^5 + 1*2^6 = 75
编辑:对于JavaScript 示例:
So I am guessing that you want
1 converted to 1,
10 converted to 2,
11 converted to 3,
etc.,
1001011 is converted to 1*2^0 + 1*2^1 + 0*2^2 + 1*2^3 + 0*2^4+ 0*2^5 + 1*2^6 = 75
EDIT: For example with JavaScript: