BitConverter如何重建字节为简短
因此,我目前正在尝试更多地了解eNDIANNESS以及如何将字节转换为短裤,INT等。
我认为我会开始简单,首先要转换一个简短的(“ 30000”)
进入两个字节并将其存储在A MemoryStream
中
private static void WriteShort(short constValue)
{
_stream.WriteByte((byte)(constValue & 255));
_stream.WriteByte((byte)(constValue >> 8));
}
,如果我正确理解事情,我首先使用位和操作员&
进行一些位掩模。
0000 0000 1111 1111 &
0111 0101 0011 0000
这将导致一个看起来像0011 0000
的字节,我会将其写入MemoryStream
。因此,现在MemoryStream
包含一个字节数组,看起来像[48]
i然后基于相同的值30000
编写另一个字节,但是我BITSHIFT右侧的字节乘以8个,以获取左手的大多数8位0111 0101(117)
,并将其作为字节写入MemoryStream
。因此,现在字节数组看起来像[48,117]
对我来说似乎很清楚,这是简短的重建,这使我有些困惑。
重建简短时,我怎么需要做相同的bitshifting?我认为这个问题与我的另一个问题有些相关,而“+”操作员如何将“ 48”和“ 117”转换为30000?
bitConverter.toint16(_Stream.toArray());
如何知道要移动的字节等以输出正确的值?
private static short ReadShort()
{
_stream.Position = 0;
return (short)((_stream.ReadByte() & 255) +
(_stream.ReadByte() << 8));
}
整个程序
internal class Program
{
private static MemoryStream _stream;
static void Main(string[] args)
{
Console.WriteLine(117 << 8);
_stream = new MemoryStream();
short constValue = 30000;
WriteShort(constValue);
var v = ReadShort();
/* True */
Console.WriteLine($"Is Little Endian: {BitConverter.IsLittleEndian}");
}
private static void WriteShort(short constValue)
{
_stream.WriteByte((byte)(constValue & 255));
_stream.WriteByte((byte)(constValue >> 8));
}
private static short ReadShort()
{
_stream.Position = 0;
return (short)((_stream.ReadByte() & 255) +
(_stream.ReadByte() << 8));
}
}
So I'm currently trying to learn more about endianness and how bytes are converted to shorts, ints etc.
And I figured I'd start simple, I'd start by converting a short ("30000")
into two bytes and storing it in a MemoryStream
private static void WriteShort(short constValue)
{
_stream.WriteByte((byte)(constValue & 255));
_stream.WriteByte((byte)(constValue >> 8));
}
and if I understand things correctly, I start by doing some bitmasking using the bitwise AND operator &
.
0000 0000 1111 1111 &
0111 0101 0011 0000
Which would result in a byte that looks like this 0011 0000
and I would write that to the MemoryStream
. So now the MemoryStream
contains a byte array which looks like this [48]
I then write another byte, based on the same value 30000
but I bitshift the bytes to the right by 8 in order to get the left hand most 8 bits 0111 0101 (117)
and write that as a byte to the MemoryStream
. So now the byte array looks like this [48, 117]
that part seems pretty clear to me, it's the reconstructing of the short which gets me slightly confused.
How come I need to do the same bitshifting when reconstructing the short? I think that question is somewhat related to my other question which is, how does the '+' operator convert '48' and '117' to 30000?
And how does the BitConverter.ToInt16(_stream.ToArray());
know what bytes to shift etc to output the correct value?
private static short ReadShort()
{
_stream.Position = 0;
return (short)((_stream.ReadByte() & 255) +
(_stream.ReadByte() << 8));
}
The entire program
internal class Program
{
private static MemoryStream _stream;
static void Main(string[] args)
{
Console.WriteLine(117 << 8);
_stream = new MemoryStream();
short constValue = 30000;
WriteShort(constValue);
var v = ReadShort();
/* True */
Console.WriteLine(quot;Is Little Endian: {BitConverter.IsLittleEndian}");
}
private static void WriteShort(short constValue)
{
_stream.WriteByte((byte)(constValue & 255));
_stream.WriteByte((byte)(constValue >> 8));
}
private static short ReadShort()
{
_stream.Position = 0;
return (short)((_stream.ReadByte() & 255) +
(_stream.ReadByte() << 8));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,您有两个字节,
还要零扩展两个值,因此两个值是16位值:
将这些数字添加在一起显然不会导致原始数字。我们还可以看到
_Stream.ReadByte()&amp; 255
在_Stream.ReadByte()
上没有做任何有意义的事情。但是,移动最后一个数字将导致,我们可以看到将这些数字添加或添加在一起会导致原始值。请注意,
bitConverter
不一定需要进行此舞蹈,它可以简单地使用本机代码直接复制字节。将较大的整数转换为字节时,您可能需要考虑 endianness ,因为整数会根据整数的表示方式有所不同在平台上。So you have two bytes,
Lets also zero extend both values so both are 16 bit values:
Just adding these numbers together would clearly not result in the original number. We can also see that
_stream.ReadByte() & 255
does not do anything meaningful over just_stream.ReadByte()
. Shifting the last number however will result inAnd we can see that adding or OR-ing these numbers together results in the original value. Note that
BitConverter
does not necessarily need to do this dance, it could simply use native code to copy bytes directly. When converting larger integers to bytes you may need to consider Endianness, since integers will be represented differently depending on platform.