zpeters

zpeters

I feel like i’m missing something really basic here but for the life of me cannot figure this out.

When i run System.cmd(“echo”, [“hello”]). I get the output i expect, namely {“hello\n”, 0}

However when i run System.cmd(“lynx”, [“–dump”, “http://google.com”]) I get something like this

{<<10, 32, 32, 32, 83, 101, 97, 114, 99, 104, 32, 91, 49, 93, 73, 109, 97, 103,
101, 115, 32, 91, 50, 93, 77, 97, 112, 115, 32, 91, 51, 93, 80, 108, 97, 121,
32, 91, 52, 93, 89, 111, 117, 84, 117, 98, 101, 32, 91, …>>, 0}

When i run lynx --dump http://google.com from a shell it appears to be a normal string that is output. I don’t understand why running this command in elixir gives me a binary result.

If this is unavoidable, is there a way to convert from this binary to a string?

Thank you in advance for the schooling! :slight_smile:

Showing Posts 1 to 1

axelson

axelson

Scenic Core Team

A String in Elixir is actually a UTF-8 encoded binary: https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html

So both of your results are binaries. One way to verify/play with this is to run

iex(30)> i "hello\n"
Term
  "hello\n"
Data type
  BitString
Byte size
  6
Description
  This is a string: a UTF-8 encoded binary. It's printed surrounded by
  "double quotes" because all UTF-8 encoded codepoints in it are printable.
Raw representation
  <<104, 101, 108, 108, 111, 10>>
Reference modules
  String, :binary
Implemented protocols
  Collectable, Ecto.DataType, Ecto.Queryable, IEx.Info, Inspect, List.Chars, Msgpax.Packer, Phoenix.HTML.Safe, Phoenix.Param, Plug.Exception, Poison.Decoder, Poison.Encoder, Slugify, String.Chars, Timber.Eventable

From there you can see the raw “binary” representation is <<104, 101, 108, 108, 111, 10>> and if you enter that into an iEX prompt you get back the string “hello\n” (note: this only works for binaries that are valid UTF-8 and all the characters are printable)

iex(31)> <<104, 101, 108, 108, 111, 10>>
"hello\n"

So for some reason lynx is returning invalid UTF-8 when run as lynx --dump http://google.com. To strip invalid characters you could do some processing like so:

iex(39)> {string, _} = System.cmd("lynx", ["--dump", "http://google.com"])
{<<10, 32, 32, 32, 83, 101, 97, 114, 99, 104, 32, 91, 49, 93, 73, 109, 97, 103,
   101, 115, 32, 91, 50, 93, 77, 97, 112, 115, 32, 91, 51, 93, 80, 108, 97, 121,
   32, 91, 52, 93, 89, 111, 117, 84, 117, 98, 101, 32, 91, ...>>, 0}
iex(40)> String.graphemes(string) |> Enum.filter(&String.valid?/1) |> Enum.join("") |> IO.puts

   Search [1]Images [2]Maps [3]Play [4]YouTube [5]News [6]Gmail [7]Drive
   [8]More
   [9]Web History | [10]Settings | [11]Sign in

   [12]Day 5 of the Doodle Snow Games!

     _______________________________________________________
   Google Search  I'm Feeling Lucky    [13]Advanced search
      [14]Language tools

   [15]Advertising Programs     [16]Business Solutions     [17]+Google
    [18]About Google

                       2018 - [19]Privacy - [20]Terms

References

   1. http://www.google.com/imghp?hl=en&tab=wi
   2. http://maps.google.com/maps?hl=en&tab=wl
   3. https://play.google.com/?hl=en&tab=w8
   4. http://www.youtube.com/?gl=US&tab=w1
   5. http://news.google.com/nwshp?hl=en&tab=wn
   6. https://mail.google.com/mail/?tab=wm
   7. https://drive.google.com/?tab=wo
   8. https://www.google.com/intl/en/options/
   9. http://www.google.com/history/optout?hl=en
  10. http://www.google.com/preferences?hl=en
  11. https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=http://www.google.com/
  12. http://www.google.com/search?site=&ie=UTF-8&q=Winter+Olympics&oi=ddle&ct=doodle-snow-games-day-7-5009413877268480-lawcta&hl=en&kgmid=/m/03tng8&sa=X&ved=0ahUKEwi5q-6Bo6TZAhVY62MKHdNJB3wQPQgC
  13. http://www.google.com/advanced_search?hl=en&authuser=0
  14. http://www.google.com/language_tools?hl=en&authuser=0
  15. http://www.google.com/intl/en/ads/
  16. http://www.google.com/services/
  17. https://plus.google.com/116899029375914044550
  18. http://www.google.com/intl/en/about.html
  19. http://www.google.com/intl/en/policies/privacy/
  20. http://www.google.com/intl/en/policies/terms/

:ok
— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews