c# netcore2.0 int to bytes conversion with endianess 支持
c# netcore2.0 int to bytes conversion with endianess support
我正在开发一个信封加密库,其中信封结构是字节数组的组合。
keyAlias length , keyAlias , IVBytes, cipherBytes =
[int][keyAlias][IV][cipherBytes]
当我存储 int 的前 4 个字节以从信封字节中检索 keyAlias 的长度时,寻找 int 到字节并返回到 int 的正确处理。
假设:我决定始终将 int 字节以 Little-Endian 格式存储在信封字节中。
以下将 int 转换为字节的方法是否正确。我无法测试它,因为无法访问 Big-Endian 机器。
[Fact]
public void IntToByteConverion_ShouldConvertBack2()
{
var intBytes = 2.ToLittleEndianBytes();
//Store intBytes to Database or AWS S3
// Retrive back the bytearray and convert to int32.
int intValue = intBytes.FromLittleEndianBytes();
Assert.Equal(2, intValue);
}
public static byte[] ToLittleEndianBytes(this int value)
{
if (BitConverter.IsLittleEndian)
return BitConverter.GetBytes(value);
else
{
var bigEndianBytes = BitConverter.GetBytes(value);
Array.Reverse(bigEndianBytes);// Converted to LittleEndian
return bigEndianBytes;
}
}
public static Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)
{
if (BitConverter.IsLittleEndian)
return BitConverter.ToInt32(littleEndianBytes, 0);
else
{
Array.Reverse(littleEndianBytes);// Converted to big endian as machine CPU is big endian
return BitConverter.ToInt32(littleEndianBytes, 0);
}
}
你所做的工作。要测试它,您可以替换:
if(BitConverter.IsLittleEndian)
与:
if (false/*BitConverter.IsLittleEndian*/)
并用调试器检查字节是否确实被颠倒了。如果是这样(确实如此)那么它就可以工作,因为 Little-Endian 和 Big-Endian 之间的区别是字节顺序。
仅对您的代码发表评论(这可能更多地属于 Code Review 的范围):
使用:
Array.Reverse(littleEndianBytes);
在方法中:
Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)
不是最佳实践,因为您要反转发送到该方法的字节数组。该方法的使用者可能没有想到并感到困惑。
我正在开发一个信封加密库,其中信封结构是字节数组的组合。
keyAlias length , keyAlias , IVBytes, cipherBytes =
[int][keyAlias][IV][cipherBytes]
当我存储 int 的前 4 个字节以从信封字节中检索 keyAlias 的长度时,寻找 int 到字节并返回到 int 的正确处理。
假设:我决定始终将 int 字节以 Little-Endian 格式存储在信封字节中。
以下将 int 转换为字节的方法是否正确。我无法测试它,因为无法访问 Big-Endian 机器。
[Fact]
public void IntToByteConverion_ShouldConvertBack2()
{
var intBytes = 2.ToLittleEndianBytes();
//Store intBytes to Database or AWS S3
// Retrive back the bytearray and convert to int32.
int intValue = intBytes.FromLittleEndianBytes();
Assert.Equal(2, intValue);
}
public static byte[] ToLittleEndianBytes(this int value)
{
if (BitConverter.IsLittleEndian)
return BitConverter.GetBytes(value);
else
{
var bigEndianBytes = BitConverter.GetBytes(value);
Array.Reverse(bigEndianBytes);// Converted to LittleEndian
return bigEndianBytes;
}
}
public static Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)
{
if (BitConverter.IsLittleEndian)
return BitConverter.ToInt32(littleEndianBytes, 0);
else
{
Array.Reverse(littleEndianBytes);// Converted to big endian as machine CPU is big endian
return BitConverter.ToInt32(littleEndianBytes, 0);
}
}
你所做的工作。要测试它,您可以替换:
if(BitConverter.IsLittleEndian)
与:
if (false/*BitConverter.IsLittleEndian*/)
并用调试器检查字节是否确实被颠倒了。如果是这样(确实如此)那么它就可以工作,因为 Little-Endian 和 Big-Endian 之间的区别是字节顺序。
仅对您的代码发表评论(这可能更多地属于 Code Review 的范围):
使用:
Array.Reverse(littleEndianBytes);
在方法中:
Int32 FromLittleEndianBytes(this byte[] littleEndianBytes)
不是最佳实践,因为您要反转发送到该方法的字节数组。该方法的使用者可能没有想到并感到困惑。