我需要将一个int转换为字节[],其中一种方法是使用BitConverter.GetBytes()。但我不确定这是否符合以下规格:
XDR有符号整数是一个32位的数据,它将整数编码为 范围[-2147483648,2147483647]。整数表示为 二的补符号。最高和最低有效字节为 分别是0和3。整数的声明如下:
来源:RFC1014 3.2
我怎么能做一个int到字节的转换,将满足上述规范?
我需要将一个int转换为字节[],其中一种方法是使用BitConverter.GetBytes()。但我不确定这是否符合以下规格:
XDR有符号整数是一个32位的数据,它将整数编码为 范围[-2147483648,2147483647]。整数表示为 二的补符号。最高和最低有效字节为 分别是0和3。整数的声明如下:
来源:RFC1014 3.2
我怎么能做一个int到字节的转换,将满足上述规范?
当前回答
为什么上面示例中的所有这些代码…
具有显式布局的结构以两种方式工作,并且没有性能影响。
更新:因为有一个关于如何处理字节序的问题,我添加了一个接口,说明如何抽象它。另一个实现结构可以处理相反的情况
public interface IIntToByte
{
Int32 Int { get; set;}
byte B0 { get; }
byte B1 { get; }
byte B2 { get; }
byte B3 { get; }
}
[StructLayout(LayoutKind.Explicit)]
public struct IntToByteLE : UserQuery.IIntToByte
{
[FieldOffset(0)]
public Int32 IntVal;
[FieldOffset(0)]
public byte b0;
[FieldOffset(1)]
public byte b1;
[FieldOffset(2)]
public byte b2;
[FieldOffset(3)]
public byte b3;
public Int32 Int {
get{ return IntVal; }
set{ IntVal = value;}
}
public byte B0 => b0;
public byte B1 => b1;
public byte B2 => b2;
public byte B3 => b3;
}
其他回答
using static System.Console;
namespace IntToBits
{
class Program
{
static void Main()
{
while (true)
{
string s = Console.ReadLine();
Clear();
uint i;
bool b = UInt32.TryParse(s, out i);
if (b) IntPrinter(i);
}
}
static void IntPrinter(uint i)
{
int[] iarr = new int [32];
Write("[");
for (int j = 0; j < 32; j++)
{
uint tmp = i & (uint)Math.Pow(2, j);
iarr[j] = (int)(tmp >> j);
}
for (int j = 32; j > 0; j--)
{
if(j%8==0 && j != 32)Write("|");
if(j%4==0 && j%8 !=0) Write("'");
Write(iarr[j-1]);
}
WriteLine("]");
}
}
}```
如果你想了解更多关于表示数字的各种方法(包括Two's Complement)的一般信息,请查看:
维基百科上2的补数和有符号数表示
这里有另一种方法:我们都知道1x字节= 8x位,而且,一个“常规”整数(int32)包含32位(4个字节)。我们可以使用>>操作符向右移位位(>>操作符不改变值)。
int intValue = 566;
byte[] bytes = new byte[4];
bytes[0] = (byte)(intValue >> 24);
bytes[1] = (byte)(intValue >> 16);
bytes[2] = (byte)(intValue >> 8);
bytes[3] = (byte)intValue;
Console.WriteLine("{0} breaks down to : {1} {2} {3} {4}",
intValue, bytes[0], bytes[1], bytes[2], bytes[3]);
byte[] Take_Byte_Arr_From_Int(Int64 Source_Num)
{
Int64 Int64_Num = Source_Num;
byte Byte_Num;
byte[] Byte_Arr = new byte[8];
for (int i = 0; i < 8; i++)
{
if (Source_Num > 255)
{
Int64_Num = Source_Num / 256;
Byte_Num = (byte)(Source_Num - Int64_Num * 256);
}
else
{
Byte_Num = (byte)Int64_Num;
Int64_Num = 0;
}
Byte_Arr[i] = Byte_Num;
Source_Num = Int64_Num;
}
return (Byte_Arr);
}
When I look at this description, I have a feeling, that this xdr integer is just a big-endian "standard" integer, but it's expressed in the most obfuscated way. Two's complement notation is better know as U2, and it's what we are using on today's processors. The byte order indicates that it's a big-endian notation. So, answering your question, you should inverse elements in your array (0 <--> 3, 1 <-->2), as they are encoded in little-endian. Just to make sure, you should first check BitConverter.IsLittleEndian to see on what machine you are running.