Zesky665

Zesky665

How to parse bitstring?

Hi,

Anyone know how to parse a bitstring?
I have values like this : <<170, 192, 65, 0, 100, 0, 61, 86, 56, 171>> coming in from a sensor.
I need to get 65 ad 100 out to of that without changing them to another number. Anyone have an idea as to how to do that?

Thanks

Marked As Solved

kip

kip

ex_cldr Core Team

Here you go:

iex> :binary.bin_to_list x
[170, 192, 65, 0, 100, 0, 61, 86, 56, 171]

Also Liked

kip

kip

ex_cldr Core Team

Yes, it is. Everything in Erlang is available in Elixir.

kip

kip

ex_cldr Core Team

Something like this:

iex> x = <<170, 192, 65, 0, 100, 0, 61, 86, 56, 171>>
iex> << _ :: bytes-4, onehundred :: bytes-1, _ :: bytes-1, sixtyone :: bytes-1, _rest :: binary >> = x          
<<170, 192, 65, 0, 100, 0, 61, 86, 56, 171>>
iex> onehundred
"d"
iex> sixtyone
"="

This assumes you want the fields as binaries. If you want them as integers:

iex> << _ :: bytes-4, onehundred :: size(8), _ :: bytes-1, sixtyone :: size(8), _rest :: binary >> = x
<<170, 192, 65, 0, 100, 0, 61, 86, 56, 171>>
iex> sixtyone
61
iex> onehundred
100

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement