You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# BitConverter字节解析原理及Unreal(C++/蓝图)实现方法

问题背景

(通过TCP协议传输data字节数据)

byte[] data = {153, 130, 0, 0 };
index = 0;
ushort nm = ExtractUshort();

public ushort ExtractUshort() {
    ushort value = BitConverter.ToUInt16(data, index);
    index += 2;
    return value;
}

Debug.Log("nm value >" + nm); 

上述代码在Unity环境中运行时,日志输出结果为nm value > 33433。


BitConverter 解析逻辑运行原理

输出33433的核心原因是小端序(Little-Endian)字节序解析规则,这也是Windows平台、Unity默认运行环境采用的字节序:

  1. 传入的前两个字节为153(对应十六进制0x99)、130(对应十六进制0x82)
  2. 小端序规则下,低内存地址存储数值低位,高内存地址存储数值高位,因此两个字节拼接为16位无符号整数时,计算逻辑为:130 * 256 + 153 = 33280 + 153 = 33433,和日志输出完全匹配。
  3. BitConverter的所有ToXXX方法本质是从字节数组指定偏移位置开始,按照当前运行环境的本机字节序,拷贝对应长度的字节直接映射为目标值类型的内存数据,不会做额外的字节序转换。

注意:Unity的BitConverter.IsLittleEndian属性在桌面端、移动端默认返回true,和TCP协议默认的大端序(网络字节序)相反,跨网络传输数据时必须先对齐字节序规则,否则会出现解析值错乱。


Unreal引擎等效实现方案

C++实现

编写通用字节解析工具类,和C# BitConverter行为完全对齐,默认按小端序解析,解析后自动偏移索引值:

// 头文件声明工具函数
class YOURMODULE_API FByteParser
{
public:
    static uint16 ExtractUShort(const TArray<uint8>& Data, int32& Index);
    static bool ExtractBool(const TArray<uint8>& Data, int32& Index);
    static float ExtractFloat(const TArray<uint8>& Data, int32& Index);
    static double ExtractDouble(const TArray<uint8>& Data, int32& Index);
    static int32 ExtractInt32(const TArray<uint8>& Data, int32& Index);
    static uint32 ExtractUInt32(const TArray<uint8>& Data, int32& Index);
};
// 源文件实现
uint16 FByteParser::ExtractUShort(const TArray<uint8>& Data, int32& Index)
{
    uint16 Value = 0;
    // 小端序拼接:低地址存数值低位
    Value = Data[Index];
    Value |= (uint16)Data[Index + 1] << 8;
    Index += 2;
    return Value;
}

bool FByteParser::ExtractBool(const TArray<uint8>& Data, int32& Index)
{
    bool Value = Data[Index] != 0;
    Index += 1;
    return Value;
}

float FByteParser::ExtractFloat(const TArray<uint8>& Data, int32& Index)
{
    float Value = 0;
    memcpy(&Value, &Data[Index], sizeof(float));
    Index += 4;
    return Value;
}

double FByteParser::ExtractDouble(const TArray<uint8>& Data, int32& Index)
{
    double Value = 0;
    memcpy(&Value, &Data[Index], sizeof(double));
    Index += 8;
    return Value;
}

int32 FByteParser::ExtractInt32(const TArray<uint8>& Data, int32& Index)
{
    int32 Value = 0;
    memcpy(&Value, &Data[Index], sizeof(int32));
    Index += 4;
    return Value;
}

uint32 FByteParser::ExtractUInt32(const TArray<uint8>& Data, int32& Index)
{
    uint32 Value = 0;
    memcpy(&Value, &Data[Index], sizeof(uint32));
    Index += 4;
    return Value;
}

调用示例(和开头C#代码逻辑完全等效):

TArray<uint8> Data = {153, 130, 0, 0};
int32 Index = 0;
uint16 nm = FByteParser::ExtractUShort(Data, Index);
UE_LOG(LogTemp, Log, TEXT("nm value > %d"), nm); // 输出结果:nm value > 33433

蓝图实现

直接使用UE自带的字节转换节点即可,不需要额外开发插件:

  • 解析16位无符号整数:使用Bytes to Uint16节点,勾选Is Little Endian选项,传入从当前索引开始截取的长度为2的字节数组,解析完成后索引值+2
  • 解析布尔值:直接取当前索引位置的字节,判断值是否不等于0,索引+1
  • 解析float/int32/uint32:分别使用Bytes to Float/Bytes to Int32/Bytes to Uint32节点,勾选Is Little Endian,传入从当前索引开始截取的长度为4的字节数组,解析完成后索引+4
  • 解析double:使用Bytes to Double节点,勾选Is Little Endian,传入从当前索引开始截取的长度为8的字节数组,解析完成后索引+8

类型转换规则对照表

和C# BitConverter行为完全对齐的解析规则如下:

  • double value = BitConverter.ToDouble(data, index);:从index位置取8字节解析为双精度浮点数,解析后index += 8
  • bool value = BitConverter.ToBoolean(data, index);:从index位置取1字节,值非0则为true,否则为false,解析后index += 1
  • float value = BitConverter.ToSingle(data, index);:从index位置取4字节解析为单精度浮点数,解析后index += 4
  • uint value = BitConverter.ToUInt32(data, index);:从index位置取4字节解析为32位无符号整数,解析后index += 4
  • int value = BitConverter.ToInt32(data, index);:从index位置取4字节解析为32位有符号整数,解析后index += 4

内容的提问来源于stack exchange,提问作者draper.kaegan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 21:21:25