二进制补码转换
我需要将二进制补码格式的字节转换为正整数字节。 范围 -128 到 127 映射到 0 到 255。
Examples: -128 (10000000) -> 0 , 127 (01111111) -> 255, etc.
编辑为了消除混淆,输入字节(当然)是 0 到 255 范围内的无符号整数。但它表示有符号整数使用二进制补码格式,范围为 -128 到 127。例如,输入字节值128(二进制10000000)实际上代表-128。
额外编辑 好吧,假设我们有以下字节流 0,255,254,1,127。在二进制补码格式中,这代表 0、-1、-2、1、127。我需要将其限制在 0 到 255 范围内。有关更多信息,请查看这篇很难找到的文章:二进制补码
I need to convert bytes in two's complement format to positive integer bytes.
The range -128 to 127 mapped to 0 to 255.
Examples: -128 (10000000) -> 0 , 127 (01111111) -> 255, etc.
EDIT To clear up the confusion, the input byte is (of course) an unsigned integer in the range 0 to 255. BUT it represents a signed integer in the range -128 to 127 using two's complement format. For example, the input byte value of 128 (binary 10000000) actually represents -128.
EXTRA EDIT Alrighty, lets say we have the following byte stream 0,255,254,1,127. In two's complement format this represents 0, -1, -2, 1, 127. This I need clamping to the 0 to 255 range. For more info check out this hard to find article: Two's complement
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
从您的示例输入中,您只需要:
From you sample input you simply want:
宾果游戏:-)
bingo :-)
尝试
或
Try
or
我相信 2s 补码字节最好用以下方法完成。也许不优雅或简短,但清晰可见。我会将其作为静态方法放在我的 util 类之一中。
I believe that 2s complement bytes would be best done with the following. Maybe not elegant or short but clear and obvious. I would put it as a static method in one of my util classes.
您可以描述一些简单的事情,例如向您的数字添加偏差(在本例中,向带符号的数字添加 128)。
You could be describing something as simple as adding a bias to your number ( in this case, adding 128 to the signed number ).
如果我理解正确,您的问题是如何转换输入,这实际上是一个
signed-byte
(sbyte),但该输入存储在无符号整数
中,然后还可以通过将其转换为来避免负值零。需要明确的是,当您使用有符号类型(例如
ubyte
)时,框架会在幕后使用Two 的补码
,因此只需转换为您将使用的正确类型二进制补码。然后,完成转换后,您可以使用简单的
if
或条件三元运算符 (?:)。对于
从 128 到 255
(或从 -128 到 -1)的值,下面提供的函数将返回0
,对于值相同的值
从 0 到 127
。因此,如果您必须使用无符号整数作为输入和输出,您可以使用如下所示的内容:
或者,恕我直言,您可以将输入和输出更改为最适合您的输入和输出的数据类型(sbyte和字节):
If I undestood correctly, your problem is how to convert the input, which is really a
signed-byte
(sbyte), but that input is stored in aunsigned integer
, and then also avoid negative values by converting them to zero.To be clear, when you use a signed type (like
ubyte
) the framework is usingTwo's complement
behind the scene, so just by casting to the right type you will be using two's complement.Then, once you have that conversion done, you could clamp the negative values with a simple
if
or a conditional ternary operator (?:).The functions presented below will return
0
for valuesfrom 128 to 255
(or from -128 to -1), andthe same value
for valuesfrom 0 to 127
.So, if you must use unsigned integers as input and output you could use something like this:
Or, IMHO, you could change your input and outputs to the data types best suited to your input and output (sbyte and byte):
异或 MSB,仅此而已
xor MSB, that's all
这是我对这个问题的解决方案,适用于大于 8 位的数字。我的示例是 16 位值。注意:您必须检查第一位,看看它是否为负数。
步骤:
通过在变量前放置“~”将 # 转换为补语。 (即 y = ~y)
将 #s 转换为二进制字符串
将二进制字符串分解为字符数组
开始对于最右边的值,添加 1 ,跟踪进位。将结果存储在字符数组中。
将字符数组转换回字符串。
Here is my solution for this problem, for numbers bigger than 8-bits. My example is for a 16-bit value. Note: You will have to check the first bit, to see if it is a negative or not.
Steps:
Convert # to compliment by placing '~' before variable. (ie. y = ~y)
Convert #s to binary string
Break binary strings into character array
Starting with right most value, add 1 , keeping track of carries. Store result in character array.
Convert character array back to string.
所以问题是OP的问题并不是真正的二进制补码转换。他向一组值添加偏差,以将范围从 -128..127 调整到 0..255。
要实际进行二进制补码转换,只需将有符号值转换为无符号值,如下所示:
-1 变为 255。-128 变为 128。但这听起来不像 OP 想要的。他只是想向上滑动一个数组,使最低的有符号值(-128)变成最低的无符号值(0)。
要添加偏差,只需进行整数加法:
So the problem is that the OP's problem isn't really two's complement conversion. He's adding a bias to a set of values, to adjust the range from -128..127 to 0..255.
To actually do a two's complement conversion, you just typecast the signed value to the unsigned value, like this:
-1 becomes 255. -128 becomes 128. This doesn't sound like what the OP wants, though. He just wants to slide an array up so that the lowest signed value (-128) becomes the lowest unsigned value (0).
To add a bias, you just do integer addition: