Skip to content

Instantly share code, notes, and snippets.

@husio
Created June 27, 2019 11:00
Show Gist options
  • Save husio/0508ad24aa52a8f95684a1a86f33e1cb to your computer and use it in GitHub Desktop.
Save husio/0508ad24aa52a8f95684a1a86f33e1cb to your computer and use it in GitHub Desktop.
package main
import (
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
func main() {
decodeFl := flag.Bool("d", false, "Decode Hex encoded value")
flag.Parse()
data := strings.Join(flag.Args(), " ")
if data == "" {
raw, _ := ioutil.ReadAll(os.Stdin)
data = string(raw)
}
if *decodeFl {
raw, err := hex.DecodeString(data)
if err != nil {
fail(err)
}
os.Stdout.Write(raw)
} else {
io.WriteString(os.Stdout, hex.EncodeToString([]byte(data)))
}
}
func fail(err error) {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment