How to undo `unsafe_string` call

I’m using a library (Instruments.jl) that does a bunch of ccalls to return a string.
It does this by calling unsafe_string(pointer(buf)) where buf isa Vector{UInt8}.

Can I undo this call to unsafe_string to just get the Vector{UInt8} back?

I want to do this because the string is actually data that I want to reinterpret, so I really just want a way to reinterpret a string, since

julia> reinterpret(Float32, "asdf")
ERROR: ArgumentError: Source type for `reinterpret` must be isbits
julia> s = "foo"
"foo"

julia> codeunits(s)
3-element Base.CodeUnits{UInt8, String}:
 0x66
 0x6f
 0x6f

might be what you want. Or something else depending on what you have available (like unsafe_wrap)

2 Likes