How to extract a binary from a string?

Asked

Viewed 63 times

3

I have the following decimal list in Erlang:
"01000001" == [48, 49, 49, 49, 49, 49, 48]

I need to create a binary type from that list, so:
<<01000001>> == <<"A">>

I tried to convert with the function list_to_binary/1, but she becomes:
<<"01000001">> == <<48, 49, 49, 49, 49, 49, 48>>

Is there a function that does this? Or how would be the best way to transform "01000001" in <<01000001>>?

  • You’re asking for two different things. The true binary value is the one you already have. The fact that you’re not seeing it doesn’t mean it’s not there. In fact everything is binary, and precisely because it is binary this is the real thing, it has nothing to do. Another thing is to want to see what the number would look like in a textual representation indicating where the bits are connected and off, then you will create a text, like the one you are reading now, which will only have two characters, one of them is what we call zero (0) and the other we call one (1). This is not real binary, it’s a text.

  • @Maniero É, I need to see a way to reformulate the question, in fact the "01000001" is the string, which is what I have, I need to convert it to binary without using the bits of characters 0 and 1, but the characters LIKE bits. I need "01000001" to be transformed into [65]. The representations Erlang uses are complicated to pick up. "A" is "string", <"A">> is binary, [65] is the same as "A", because both are decimal lists.

  • You still don’t understand why there’s no such thing as binary that you’re trying to convert. Or it has a text that is represented with bits or has a number and it doesn’t matter how it is represented. This number can even be visualized as a letter, but it is a question of how it presents, not how it is in memory. If you want me to present in any specific way, that’s all you have to talk about, not how to convert something.

  • I will try to rewrite the question, I saw now that until the title ended up erasing a word, my question is about types and not presentation, what I have and what I want if I try to present having the same type, they will be different.... But I’ll change the question

1 answer

0

If you need to "transform" "01000001" in <<"01000001">>, use the function list_to_binary/1. Take an example:

erl(0)> Binary = list_to_binary("01000001").
<<"01000001">>

erl(1)> binary_to_integer(Binary, 2).       
65

Remembering that <<"01000001">> is a syntactic sugar for <<48, 49, 48, 48, 48, 48, 48, 49>>, and this is solved at compile time. Such that if we try the following, we receive an error:

erl(0)> Str = "01000001".
erl(1)> <<Str>>.         
** exception error: bad argument
     in function  eval_bits:eval_exp_field1/6 (eval_bits.erl, line 108)
     in call from eval_bits:eval_field/3 (eval_bits.erl, line 99)
     in call from eval_bits:expr_grp/4 (eval_bits.erl, line 68)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.