Scala 3 conversions to/from hexadecimal strings.
Find a file
Pat Garrity f244969b1f
All checks were successful
/ Build and Release Library (push) Successful in 1m53s
(patch) Update to Scala version 3.5.1 (#6)
Reviewed-on: #6
2024-10-12 02:44:09 +00:00
.forgejo/workflows (patch) Fixed coverageOff ordering. (#5) 2024-04-27 02:04:12 +00:00
project (patch) Update to Scala version 3.5.1 (#6) 2024-10-12 02:44:09 +00:00
src (patch) Minor cleanup and documentation. (#4) 2024-04-27 01:46:51 +00:00
.gitignore (patch) initial, complete, hex implementation 2024-04-09 08:01:18 -05:00
.pre-commit-config.yaml (patch) initial, complete, hex implementation 2024-04-09 08:01:18 -05:00
.scalafmt.conf (patch) initial, complete, hex implementation 2024-04-09 08:01:18 -05:00
build.sbt (patch) Update to Scala version 3.5.1 (#6) 2024-10-12 02:44:09 +00:00
LICENSE Full support for MIT license. (#1) 2024-04-12 13:10:38 +00:00
README.md (patch) Minor cleanup and documentation. (#4) 2024-04-27 01:46:51 +00:00

gs-hex

GS Open Source | License (MIT)

Hexadecimal conversions for Scala 3. Provides support for standard types.

Usage

Dependency

This artifact is available in the Garrity Software Maven repository.

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).

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

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

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? Consider Donating to Pat on Ko-fi.