Skip to content

Casting byte array to Float32 #2624

Answered by ericqu
jblindsay asked this question in Q&A
Discussion options

You must be logged in to vote

I am not sure if that is the intended way to do that; I am just trying things out. I used bitcast and reused the SIMD type to force a cast. To illustrate, I start from a Float32 to an array of uint8 (byte) and from that array back to a Float32.

from memory.unsafe import bitcast

fn main () -> None :
    var temp_array: SIMD[DType.uint8, 4] = bitcast[DType.uint8, 4](SIMD[DType.float32, 1](3.1))
    print(temp_array)
    var test_float32: Float32 = bitcast[DType.float32, 1](temp_array)
    print(test_float32)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jblindsay
Comment options

Answer selected by jblindsay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants