XDR.Int - Integer
An XDR signed integer is a 32-bit datum that encodes an integer in the range [-2_147_483_648, 2_147_483_647]
.
Usage
Encoding
iex(1)> XDR.Int.new(564) |> XDR.Int.encode_xdr()
{:ok, <<0, 0, 2, 52>>}
iex(1)> XDR.Int.new(564) |> XDR.Int.encode_xdr!()
<<0, 0, 2, 52>>
Decoding
iex(1)> XDR.Int.decode_xdr(<<0, 0, 2, 52>>)
{:ok, {%XDR.Int{datum: 564}, <<>>}}
iex(1)> XDR.Int.decode_xdr!(<<0, 0, 2, 52>>)
{%XDR.Int{datum: 564}, <<>>}
Custom Integer example
defmodule CustomInt do
@type t :: XDR.Int.t()
defdelegate new(num), to: XDR.Int
defdelegate encode_xdr(int), to: XDR.Int
defdelegate encode_xdr!(int), to: XDR.Int
defdelegate decode_xdr(bytes), to: XDR.Int
defdelegate decode_xdr!(bytes), to: XDR.Int
end
# Encode
CustomInt.new(123) |> CustomInt.encode_xdr()
# Decode
CustomInt.decode_xdr!(<<0, 0, 2, 52>>)