skylord

skylord

Converting to ASCII

Hello,

I haven’t been able to find documentation on converting characters/a string’s characters to ascii. I found that the ? mark works in the iex terminal, but not in code.

I tried this with no luck:

def words_to_marks(s) do
    s
    |> String.graphemes()
    |> Enum.map(fn x -> ?x end)
  end```

Any help would be very appreciated. Thank you.

Marked As Solved

kokolegorille

kokolegorille

Hello and welcome,

There is a simple solution… to_charlist()

?a works, but ?“a” will not, so ?x will not work.

iex> "Hello" |> to_charlist() |> IO.inspect(charlists: :as_lists)
[72, 101, 108, 108, 111]
iex> "你好" |> to_charlist() |> IO.inspect(charlists: :as_lists) 
[20320, 22909]

charlist are list of codepoints

Also Liked

ericgray

ericgray

Erlang :binary.first() will convert to ascii

def to_ascii(string) when is_binary(string) do
  :binary.first(string)
end
NobbZ

NobbZ

? does not give you anything ASCII related, it is a different form to write an integer, and it works in iex and in code, but it does not work with variables. The compiler sees ?x and sees it as the integer representing the unicode code point of Latin Small Letter X, which is 120. Coincidentally this is indeed the same as the ASCII-code for the lower case x.

Also the ways shown here won’t work for any codepoint greater than 127, as those get encoded differently in the binary, anyway, ASCII has only 7 bit and therefore no ASCII code beyond 127 exists, but you should be aware of that.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement