diff options
author | rsiddharth <s@ricketyspace.net> | 2020-12-05 20:37:33 -0500 |
---|---|---|
committer | rsiddharth <s@ricketyspace.net> | 2020-12-05 20:37:33 -0500 |
commit | ea9f4e4e3aeaa78826ee54a52f4ffd273024a3a3 (patch) | |
tree | cd7c766709c50b05ddb6aee00053c0fc06f3d8e2 | |
parent | 425ed8a3a89a829ba456a0d877d3871820a88216 (diff) |
lib/hex: add HexStrToBytes
-rw-r--r-- | lib/hex.go | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -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 { |