将负整数类型写入二进制文件! [英] writing negative integer type to binary files!

查看:94
本文介绍了将负整数类型写入二进制文件!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试根据这种算法找出如何将负数写入二进制文件(对不起,我不知道格式的名称,请让我知道该格式是否有任何名称):

Hi guys,

I am trying to figure out how to write negative numbers to a binary file, following this algorithm (sorry I don''t know the name of the format, Please let me know if there is any name for this format):

while ((value & 0xFFFFFF80) != 0)
           {
               stream.WriteByte((byte)((value & 0x7F) | 0x80));
               value >>= 7;
           }
           stream.WriteByte((byte)value);


该值是整数.例如,如果value是258,它将在文件中写入两个字节[130]和[2]!

现在我的问题是如何将其扩展为负数!
还有这种格式的名称吗?

谢谢.


which value is an integer. For ex if value is 258, it will write two bytes [130] and [2] in the file!

Now my question is how can I extend this to negative number!
And also is there any name for this format?

Thanks.

推荐答案

好,我来拿你的代码-有趣的算法,但是我不明白:
(a& b)| c =(a | c)& (b | c)
(值& 0x7F)| 0x80 =(值| 0x80)& 0xFF =值| 0x80(仅考虑最后8位)
(分配属性)
那么& 0xFF"是什么呢?我以为(字节)"会截断开头的位,不是吗?

原则上,每个字节都以1开头,但最后一个始终为0.
因此可以重组整数,否则无法确定要读取的字节数.
如果该值较小,则仅需要几个字节(最佳情况下为1个字节),如果该值较大,则将使用更多字节.
在最坏的情况下(由于每个字节只能存储7位,因为第8位用于定向),您将需要32/7 = 4.57-> 5个字节.

因此,如果您知道获得较小的值比获得较大的值更有可能,则此算法可能很好.但是,当我们生活在一个无限存储的世界中时,我不会将它用于微电子设备(每个字节都很重要)之外的其他东西.

恕我直言,编写负数时无需考虑任何问题,因为这些位仅是反转的(两个"s"补码)-不论将uint还是int用作值",都没有关系.
我看到的唯一问题是,小的负数和小的正数都需要更多的存储空间,因为-1(从-127到127)等于11111111.
OK, I''ll get your code - interesting algorithm, but something I don''t understand:
(a & b) | c = (a | c) & (b | c)
(value & 0x7F) | 0x80 = (value | 0x80) & 0xFF = value | 0x80 (considering only the last 8 bits)
(Distributive property)
So for what is the "& 0xFF"? I thought the "(byte)" truncates the leading bits anyway, isn''t it?

In principle each byte starts with an 1 except the last which is always 0.
So the integer can be reassembled, otherwise the number of bytes to read cannot be determined).
If the value is small, only a few bytes are needed (best case 1 Byte), if the value is big, a lot more are used.
In the worst case (as you can only store 7 bits per byte, because the 8th bit is used for orientation) you will need 32 / 7 = 4.57 -> 5 bytes.

So if you know that it is more probable to get small values than bigger ones, this algorithm can be good. But I wouldn''t use it for something else than micro-eletronics (where each byte counts) as we live in a world with unlimited storage...

You have IMHO nothing to consider when writing negative numbers, as the bits are only inverted (two''s-complement) - it doesn''t matter whether you use uint or int for "value".
The only problem I see is, that you need a lot more storage for small negative numbers as for small positive numbers, as -1 (from -127 to 127) is equivalent to 11111111.


如果您的值是258,则无论如何它都不适合一个字节-一个字节是8位,并且可以容纳127到-128之间的数字,如果没有符号,则可以容纳0-255.

如果您的value是代码所暗示的32位整数,则它将始终在输出文件中需要4个字节-否则,您将无法存储变量value可以容纳的所有数字范围.
尝试将其作为字节数组写入文件-这样会使事情变得简单得多!
If your value is 258, then it won''t fit in a byte anyway - a byte is eight bits, and can hold numbers between 127 and -128, or 0 - 255 if it is unsigned.

If your value is a 32 bit integer as your code implies, then it will always require 4 bytes in your output file - otherwise you cannot store the full range of numbers that the variable value can hold.

Try writing it to the file as a array of bytes - it makes things a lot easier!
int value = -12345678;
byte[] bytes = BitConverter.GetBytes(value);
int rebuilt = BitConverter.ToInt32(bytes, 0);



bytes将包含四个字节:



bytes will hold four bytes:

Decimal: 178 158 067 255 
Hex:      B2  9E  43  FF

,它与-12345678的二进制等效(小尾数表示法,通常对于.NET和PC来说是正常的)

Which is the binary equivalent of -12345678 (Little endian notation, which is normal for .NET and PC''s generally)


您遇到的问题该字节是无符号的.要获得数字的负数,请对数字进行一个补数,对于短整数,该数字为xFFFFFF ^ positiveInteger (请注意,补数不是唯一的选择,而是常见的选择).知道负数的方式是,数字的第一位是1.显然,您是在串行端口上发送它的.对于负数,可能所有租赁位都必须为1英寸:转换时,需要确保对于负值,所有前导位都为1英寸.需要一些可以转换回来的算法的想法.


The problem you have is that byte is unsigned. To get the negative of a number you do the one''s complement of a number, which would be xFFFFFF ^ positiveInteger for a short integer (note one''s complement is not the only option, but the common one). The way a negative number is known is that the first bit of the number is a 1. Obviously you are sending this on a serial port. Probably all leasing bits have to be 1''s for a negative number: Need to make sure that for negative values all leading bits are 1''s when converting back. Need some idea of the algoithm that converts back.


long value1 = convert.ToInt64(value);
for (int i; i < 5; i++) //5 iterations (5 * 7 = 35
{
  stream.WriteByte((byte)((value & 0x7F) | 0x80));
  value >>= 7;
}
stream.WriteByte((byte)value);


这篇关于将负整数类型写入二进制文件!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆