简体   繁体   中英

Extract float32 from TArray<uint8>

Unreal Engine (C++)

Hi, I have a TArray of Bytes from TCP connection. I have 58 Bytes of header and 12 x 4 Bytes of Float32. I need to extract the 12 float32 numbers from my Array Bytes, I have tried this code for extracting the first number but the result is wrong every time:

float ReceivedUE4float32;
ReceivedUE4float32 = float(ReceivedData[58]); //58 index of first float32
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Dato intdex 58 ~> %f"), ReceivedUE4float32));

Can someone help me?

Socket: https://github.com/openigtlink/OpenIGTLink/blob/release-3.0/Documents/Protocol/index.md

Transform (12x4 Bytes): https://github.com/openigtlink/OpenIGTLink/blob/release-3.0/Documents/Protocol/transform.md

float(ReceivedData[58]) will dereference the 58th byte from ReceivedData and create a float from that value, which is not what you want.

You can use reinterpret_cast to read the data:

float value = *(reinterpret_cast<float*>(ReceivedData + 58));

You did not mention which platforms you are targeting, but keep in mind this pays no attention to endianness.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM