add toBytes (#14)

This commit is contained in:
Jacek Sieka 2019-12-10 15:39:57 +01:00 committed by Mamy Ratsimbazafy
parent 1c4293b3e7
commit cf4df3dc50
1 changed files with 8 additions and 2 deletions

View File

@ -76,10 +76,10 @@ func hexToPaddedByteArray*[N: static[int]](hexStr: string): array[N, byte] =
shift = 4 shift = 4
doAssert hexStr.len - p <= maxStrSize doAssert hexStr.len - p <= maxStrSize
if sz < maxStrSize: if sz < maxStrSize:
# include extra byte if odd length # include extra byte if odd length
bIdx = result.len - (sz + 1) div 2 bIdx = result.len - (sz + 1) div 2
# start with shl of 4 if length is even # start with shl of 4 if length is even
shift = 4 - sz mod 2 * 4 shift = 4 - sz mod 2 * 4
@ -123,3 +123,9 @@ func toHex*[N: static[int]](ba: array[N, byte]): string {.inline.} =
## Output is in lowercase ## Output is in lowercase
## No "endianness" reordering is done. ## No "endianness" reordering is done.
toHexAux(ba) toHexAux(ba)
func toBytes*(s: string): seq[byte] =
## Convert a string to the corresponding byte sequence - since strings in
## nim essentially are byte sequences without any particular encoding, this
## is almost a noop
cast[seq[byte]](s)