keycard-go/hexutils/hexutils.go

32 lines
661 B
Go
Raw Normal View History

2018-09-27 09:23:12 +00:00
package hexutils
import (
"encoding/hex"
"fmt"
"log"
"regexp"
)
2018-10-05 14:40:32 +00:00
// HexToBytes convert a hex string to a byte sequence.
// The hex string can have spaces between bytes.
2018-09-27 09:23:12 +00:00
func HexToBytes(s string) []byte {
s = regexp.MustCompile(" ").ReplaceAllString(s, "")
b := make([]byte, hex.DecodedLen(len(s)))
_, err := hex.Decode(b, []byte(s))
if err != nil {
log.Fatal(err)
}
return b[:]
}
2018-10-05 14:40:32 +00:00
// BytesToHexWithSpaces returns an hex string of b adding spaces between bytes.
2018-09-27 09:23:12 +00:00
func BytesToHexWithSpaces(b []byte) string {
return fmt.Sprintf("% X", b)
}
2018-10-05 14:40:32 +00:00
// BytesToHex returns an hex string of b.
2018-09-27 09:23:12 +00:00
func BytesToHex(b []byte) string {
return fmt.Sprintf("%X", b)
}