LoginSignup
6
2

More than 5 years have passed since last update.

[Elixir] 2進数で書かれたメッセージを文字に変換する

Last updated at Posted at 2018-02-09

↓以下の記事で紹介されていた Humans Not Invited というサイトですが
https://gigazine.net/news/20180208-humans-not-invited-captcha/

meta description を見てみると2進数で説明が書いてあることに気づきました :sparkles:

スクリーンショット 2018-02-09 18.36.22.png

<meta name="description" content="011010000111010101101101011000010110111001110011001000000110111001101111011101000010000001101001011011100111011001101001011101000110010101100100001011000010000001110111011001010110110001100011011011110110110101100101001000000110001001101111011101000111001100100001">

何が書いてあるか気になったので、Elixirを使って調べてみる :thinking:

for使ったバージョン

desc = "011010000111010101101101011000010110111001110011001000000110111001101111011101000010000001101001011011100111011001101001011101000110010101100100001011000010000001110111011001010110110001100011011011110110110101100101001000000110001001101111011101000111001100100001"

for <<x::binary-8 <- desc>> do
  String.to_integer x, 2
end
#'humans not invited, welcome bots!'

パイプ演算子使って書いたバージョン

"011010000111010101101101011000010110111001110011001000000110111001101111011101000010000001101001011011100111011001101001011101000110010101100100001011000010000001110111011001010110110001100011011011110110110101100101001000000110001001101111011101000111001100100001"
|> String.codepoints |> Enum.chunk(8) |> Enum.map(&Enum.join/1) |> Enum.join(&String.to_integer(&1, 2))
#'humans not invited, welcome bots!'

やってることは2進数の文字列を8ビットごとの配列に変換し、それを更に整数の配列に変換しているだけですが、
iex上で実行すると 文字列として表示してくれます ( なんでだろう)

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2