gs-hex/README.md
Pat Garrity b9ca2b5b11
All checks were successful
/ Build and Release Library (push) Successful in 1m24s
(patch) Minor cleanup and documentation. (#4)
Reviewed-on: #4
2024-04-27 01:46:51 +00:00

72 lines
1.8 KiB
Markdown

# gs-hex
[GS Open Source](https://garrity.co/oss.html) |
[License (MIT)](./LICENSE)
Hexadecimal conversions for Scala 3. Provides support for standard types.
- [Usage](#usage)
- [Dependency](#dependency)
- [Byte Array Conversion](#byte-array-conversion)
- [Type Classes](#type-classes)
- [Donate](#donate)
## Usage
### Dependency
This artifact is available in the Garrity Software Maven repository.
```scala
externalResolvers +=
"Garrity Software Releases" at "https://maven.garrity.co/gs"
val GsHex: ModuleID =
"gs" %% "gs-hex-v0" % "$VERSION"
```
### Byte Array Conversion
This library is based on two functions: `Hex.toHexString(Array[Byte])` and
`Hex.fromHexString(String)`.
```scala
import gs.hex.v0.Hex
val data: Array[Byte] = ???
val encoded: String = Hex.toHexString(data)
val decoded: Option[Array[Byte]] = Hex.fromHexString(encoded)
```
### Type Classes
This library provides `HexEncode[A]` and `HexDecode[A]` for encoding and
decoding arbitrary types as Hexadecimal strings. The types `Array[Byte]`,
`String`, `Boolean`, `Int` and `Long` are supported by default.
#### Example Type Class Implementations
```scala
given HexEncode[Array[Byte]] = new HexEncode[Array[Byte]] {
def toHexString(data: Array[Byte]): String = Hex.toHexString(data)
}
given HexDecode[Array[Byte]] = new HexDecode[Array[Byte]] {
def fromHexString(data: String): Option[Array[Byte]] = Hex.fromHexString(data)
}
```
#### Example Type Class Usage
```scala
def encode[A](data: A)(using HexEncode[A]): String =
Hex.toHexString(dataToBytes(data))
def decode[A](data: String)(using HexDecode[A]): Option[A] =
Hex.fromHexString(data).map(dataFromBytes)
```
## Donate
Enjoy this project or want to help me achieve my [goals](https://garrity.co)?
Consider [Donating to Pat on Ko-fi](https://ko-fi.com/gspfm).