将4个字节unsigned ints转换为0-127
我需要转换从另一个软件中收到的一系列值。
它们来自4个字节整数(0-0xffffffff),但我想将它们显示为0-127的范围。
我不熟悉未签名的INT的工作方式。当我尝试从软件内部设置值为127时,我会在应用程序中读取“ -16”值。
您能否给我解释一下它是如何工作的或给我一些相关资源学习的?
我更喜欢语言不可知的答案,但我碰巧使用JavaScript。
I need to convert a range of values I receive from another software.
They come in the from of 4 byte integers (0 - 0xFFFFFFFF) but I want to display them as a range of 0-127.
I'm not familiar with how unsigned ints work. When I tried setting the value to 127 from inside the software I get a reading of "-16" value in my application.
Could you please give me an explanation of how this works or give me some relevant resources to study?
I'd prefer language agnostic answers but I happen to use Javascript.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,4个字节整数可以包含范围超过40亿的值。目前尚不清楚您打算如何用0-127的范围表示这些。如果可以保证来自其他程序的值始终在0-127范围内,那么当然,这将不是问题。但是,您需要考虑收到较大的值(或负值)时会发生什么。
其次,可以签署或未签名整数。 8位变量(一个字节)可以具有签名的范围-128至127或无符号范围0到255(这可能代表实际数字,或者代表ASCII字符或一个位字符或任何其他数字的事物。必须提前知道该值表示什么以及是否签名或未签名。)
假设它只是代表一个数字。如果是一个签名的数字,则将设置为负数的最重要位,并清除正数。对于未签名的整数,最重要的位与其他任何位相同。
与8位字节保持在一起,值为0b10000001是:
未签名:129
签名:-127
无论该值是签名还是未签名,0B00000001的值将始终为1。互联网上会有很多教程讨论此主题。
第三,您将需要知道您收到的4个字节是“小”或“大居民”。换句话说,当您获得第一个字节时,这是最重要的字节还是最不重要的字节?如果您以错误的方式弄清楚了,那么结果编号将毫无意义。
在给出任何有意义的答案之前,您将需要提供更多信息。但是,我希望我能够将您指向正确的方向。
Firstly, a 4 byte integer can contain values spanning a range of over 4 billion. It is not clear as to how you intend to represent these with a range of 0-127. If the values coming from the other program can be guaranteed to always be in the range 0-127 then that of course, won't be an issue. However you will need to consider what happens when larger values (or negative values) are received.
Secondly, an integer can be signed or unsigned. An 8 bit variable (one byte) can have the signed range -128 through 127 or the unsigned range 0 through 255. (This may represent actual numbers, or maybe an ASCII character or a bit-field or any other number of things. You must know ahead of time what the value represents and if it is signed or unsigned.)
Let's assume it simply represents a number. If it is a signed number then the most significant bit will be set for negative numbers and cleared for positive numbers. For unsigned integers, the most significant bit is just the same as any other bit.
Staying with the 8-bit byte, a value of 0b10000001 would be:
unsigned: 129
signed: -127
A value of 0b00000001 will always be 1 whether the value is signed or unsigned. There will be plenty of tutorials on the internet that discuss this subject.
Thirdly, you will need to know if the 4 bytes you are receiving are "little-endian" or "big-endian". In other words, when you get the first byte, is this the most significant byte or the least significant byte? If you get this around the wrong way then the resulting number will be meaningless.
Before any meaningful answer can be given you will need to provide much more information. Nonetheless, I hope I have been able to point you in the right direction.