Demonstration of encoding/decoding shift-jis in Elixir using the Codepagex library.
Find a file
2022-01-01 20:10:10 +09:00
config Configure to encode/decode shift-jis 2022-01-01 20:10:10 +09:00
lib Configure to encode/decode shift-jis 2022-01-01 20:10:10 +09:00
test Configure to encode/decode shift-jis 2022-01-01 20:10:10 +09:00
.formatter.exs mix new shift_jis 2022-01-01 18:41:37 +09:00
.gitignore mix new shift_jis 2022-01-01 18:41:37 +09:00
mix.exs Add codepagex 2022-01-01 19:10:25 +09:00
mix.lock Add codepagex 2022-01-01 19:10:25 +09:00
README.md Configure to encode/decode shift-jis 2022-01-01 20:10:10 +09:00

SHIFT_JIS in Elixir with Codepagex

A demonstration of how to encode/decode SHIFT_JIS in Elixir with the Codepagex library

Config

SHIFT_JIS is called VENDORS/MICSFT/WINDOWS/CP932 in Codepagex. Enable it in config:

config :codepagex, :encodings, [
  # CP392 is SHIFT_JIS
  # https://en.wikipedia.org/wiki/Code_page_932_(Microsoft_Windows)
  # Make sure to `mix deps.compile codepagex --force` after changing this
  "VENDORS/MICSFT/WINDOWS/CP932"
]

Encode/Decode

After that we can encode/decode:

defmodule ShiftJis do
  # Check config/config.exs to see how to enable this
  @shift_jis "VENDORS/MICSFT/WINDOWS/CP932"

  @doc ~S"""
      iex> test = ShiftJis.encode("テスト")
      <<131, 101, 131, 88, 131, 103>>
      iex> ShiftJis.decode(test)
      "テスト"
  """

  def encode(str), do: Codepagex.from_string!(str, @shift_jis)
  def decode(str), do: Codepagex.to_string!(str, @shift_jis)
end