# `HL7v2.Encoder`
[🔗](https://github.com/Balneario-de-Cofrentes/hl7v2/blob/v3.10.1/lib/hl7v2/encoder.ex#L1)

Serializes HL7v2 messages to wire format.

Reconstructs the HL7v2 wire format from a `RawMessage` struct. The encoder
handles the MSH-1/MSH-2 special encoding and faithfully reproduces the
field structure present in the raw message. Round-tripping is canonical:
the parser normalizes line endings to CR and the encoder always appends a
trailing CR, so `parse(text) |> encode()` produces canonical wire form.

Uses iodata internally for performance — the final result is converted to
a binary only at the top level.

## Examples

    iex> {:ok, msg} = HL7v2.Parser.parse("MSH|^~\\&|SEND|FAC||RCV||20240101||ADT^A01|123|P|2.5\r")
    iex> HL7v2.Encoder.encode(msg)
    "MSH|^~\\&|SEND|FAC||RCV||20240101||ADT^A01|123|P|2.5\r"

# `encode`

```elixir
@spec encode(HL7v2.RawMessage.t()) :: binary()
```

Encodes a `RawMessage` to its HL7v2 wire format binary.

Each segment is terminated by the segment separator (CR by default).
MSH-1 and MSH-2 receive special treatment to match the standard encoding.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
