jnnks

jnnks

Binaries and Strings Length Confusion

I am reading a video file and want to create an Nx Tensors for each frame:

frames = file_contents # list of binaries
  |> Enum.map(fn frame -> 
     Nx.from_binary(frame, {:u, 8}) |> Nx.reshape({h, w, 3})
  end)

The tensor creation works, but the reshaping fails.
The bitstrings I am reading all have a different size according to byte_size, but String.length returns the expected length (width * height * 3 byte) for all frames equally.
The max difference in count between frames according to byte_size is about 7% (ca 42kb).

Questions

  • Why is there such a big difference between something seemingly equal?
  • How do I need to create the tensor, such that I can reshape it?

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

byte_size is the number of bytes. String.length/1 returns the number of Unicode graphemes in a UTF-8 string.. I’m not sure what your reshaping function is doing, but this is likely related to the cause.

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
iex(1)> <<34,34,23,55>>
<<34, 34, 23, 55>>
iex(2)> "asbvc" |> inspect(binaries: :as_binaries)
"<<97, 115, 98, 118, 99>>"

They just aren’t the same thing at all? What does “convert” mean here?

EDIT: Re-reading your question, are you asking just in general about binaries and strings or those specific binaries?

If it’s about them in general Binaries in Elixir on Exercism talks through things, as does Binaries, strings, and charlists — Elixir v1.20.2

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement