如何在不使用任何系统辅助函数的情况下将 byte[] 转换为 c# 中的 int?
How can I convert a byte[] to an int in c# without using any System helper functions?
要将 byte[] 转换为 int,我通常会使用 BitConverter.ToInt32
,但我目前在一个名为 UdonSharp 的框架内工作,该框架限制对大多数 System
方法的访问,所以我无法使用该辅助函数。我能够像这样轻松地手动进行反向操作(将 int 转换为 byte[]):
private byte[] GetBytes(int target)
{
byte[] bytes = new byte[4];
bytes[0] = (byte)(target >> 24);
bytes[1] = (byte)(target >> 16);
bytes[2] = (byte)(target >> 8);
bytes[3] = (byte)target;
return bytes;
}
但我正在努力让它以相反的方式工作。非常感谢任何帮助!
您可以在此处查看代码:https://referencesource.microsoft.com/#mscorlib/system/bitconverter.cs
#if BIGENDIAN
public static readonly bool IsLittleEndian /* = false */;
#else
public static readonly bool IsLittleEndian = true;
#endif
[System.Security.SecuritySafeCritical] // auto-generated
public static unsafe int ToInt32(byte[] value, int startIndex) {
if (value == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if ((uint) startIndex >= value.Length) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
if (startIndex > value.Length - 4) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
Contract.EndContractBlock();
fixed(byte * pbyte = & value[startIndex]) {
if (startIndex % 4 == 0) { // data is aligned
return *((int * ) pbyte);
} else {
if (IsLittleEndian) {
return ( * pbyte) | ( * (pbyte + 1) << 8) | ( * (pbyte + 2) << 16) | ( * (pbyte + 3) << 24);
} else {
return ( * pbyte << 24) | ( * (pbyte + 1) << 16) | ( * (pbyte + 2) << 8) | ( * (pbyte + 3));
}
}
}
}
要将 byte[] 转换为 int,我通常会使用 BitConverter.ToInt32
,但我目前在一个名为 UdonSharp 的框架内工作,该框架限制对大多数 System
方法的访问,所以我无法使用该辅助函数。我能够像这样轻松地手动进行反向操作(将 int 转换为 byte[]):
private byte[] GetBytes(int target)
{
byte[] bytes = new byte[4];
bytes[0] = (byte)(target >> 24);
bytes[1] = (byte)(target >> 16);
bytes[2] = (byte)(target >> 8);
bytes[3] = (byte)target;
return bytes;
}
但我正在努力让它以相反的方式工作。非常感谢任何帮助!
您可以在此处查看代码:https://referencesource.microsoft.com/#mscorlib/system/bitconverter.cs
#if BIGENDIAN
public static readonly bool IsLittleEndian /* = false */;
#else
public static readonly bool IsLittleEndian = true;
#endif
[System.Security.SecuritySafeCritical] // auto-generated
public static unsafe int ToInt32(byte[] value, int startIndex) {
if (value == null) {
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value);
}
if ((uint) startIndex >= value.Length) {
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.startIndex, ExceptionResource.ArgumentOutOfRange_Index);
}
if (startIndex > value.Length - 4) {
ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);
}
Contract.EndContractBlock();
fixed(byte * pbyte = & value[startIndex]) {
if (startIndex % 4 == 0) { // data is aligned
return *((int * ) pbyte);
} else {
if (IsLittleEndian) {
return ( * pbyte) | ( * (pbyte + 1) << 8) | ( * (pbyte + 2) << 16) | ( * (pbyte + 3) << 24);
} else {
return ( * pbyte << 24) | ( * (pbyte + 1) << 16) | ( * (pbyte + 2) << 8) | ( * (pbyte + 3));
}
}
}
}