View Source QRNBU.Encoders.Charset (NBU payment QR v0.3.3)
Character set encoding for NBU QR codes.
Supports two encoding modes as per NBU Resolution No. 97:
- UTF-8 (code 1): Unicode encoding
- CP1251 (code 2): Windows-1251 Cyrillic encoding
Character Restrictions
Both encodings have character restrictions:
- Allowed range: ASCII 32-255
- Excluded in UTF-8: 127 (DEL)
- Excluded in CP1251: 127 (DEL), 152, 160
Examples
iex> QRNBU.Encoders.Charset.encode("Оплата", :utf8)
{:ok, "Оплата", 1}
iex> QRNBU.Encoders.Charset.encode("Оплата", :cp1251)
{:ok, <<...>>, 2}
Summary
Functions
Encodes a string using the specified character encoding.
Gets the encoding code for a given encoding type.
Gets the encoding type for a given encoding code.
Types
@type encoding() :: :utf8 | :cp1251
@type encoding_code() :: 1 | 2
Functions
@spec encode(String.t(), encoding()) :: {:ok, binary(), encoding_code()} | {:error, String.t()}
Encodes a string using the specified character encoding.
Returns {:ok, encoded_binary, encoding_code} or {:error, reason}.
Parameters
string- The string to encodeencoding- Either:utf8or:cp1251
Returns
{:ok, binary, 1}for UTF-8 encoding{:ok, binary, 2}for CP1251 encoding{:error, reason}if encoding fails
Examples
iex> QRNBU.Encoders.Charset.encode("Payment", :utf8)
{:ok, "Payment", 1}
iex> QRNBU.Encoders.Charset.encode("Оплата", :cp1251)
{:ok, <<...>>, 2}
@spec encoding_code(encoding()) :: encoding_code()
Gets the encoding code for a given encoding type.
Examples
iex> QRNBU.Encoders.Charset.encoding_code(:utf8)
1
iex> QRNBU.Encoders.Charset.encoding_code(:cp1251)
2
@spec encoding_type(encoding_code()) :: encoding() | nil
Gets the encoding type for a given encoding code.
Examples
iex> QRNBU.Encoders.Charset.encoding_type(1)
:utf8
iex> QRNBU.Encoders.Charset.encoding_type(2)
:cp1251