From ea9f4e4e3aeaa78826ee54a52f4ffd273024a3a3 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sat, 5 Dec 2020 20:37:33 -0500 Subject: lib/hex: add HexStrToBytes --- lib/hex.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'lib/hex.go') diff --git a/lib/hex.go b/lib/hex.go index d680a28..e5540ce 100644 --- a/lib/hex.go +++ b/lib/hex.go @@ -44,6 +44,25 @@ func HexStrToAsciiStr(h string) string { return a } +// 'h' must be hex encoded string. +func HexStrToBytes(h string) []byte { + lh := len(h) + + if lh < 1 { + return []byte{} + } + if lh == 1 { + return []byte{byte(HexCharToDec(h[0]))} + } + + bs := make([]byte, 0) + for i := 0; i < lh; i += 2 { + b := HexCharToDec(h[i])<<4 | HexCharToDec(h[i+1]) + bs = append(bs, byte(b)) + } + return bs +} + func AsciiStrToHexStr(as string) string { hs := "" if len(as) < 1 { -- cgit v1.2.3