Created
November 11, 2018 23:38
-
-
Save nkcmr/d57182ab0a4361e110d97665224fcfeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"math" | |
"os" | |
"strconv" | |
"strings" | |
"text/template" | |
) | |
const rowWidth = 8 | |
var t = template.Must(template.New("main").Parse(` | |
var filedata = []byte{ | |
{{- range .Data}} | |
{{.}}, | |
{{- end}} | |
} | |
`)) | |
func main() { | |
data, err := ioutil.ReadAll(os.Stdin) | |
if err != nil { | |
panic(err) | |
} | |
rows := make([]string, int(math.Ceil(float64(len(data)/rowWidth)))) | |
k := -1 | |
buf := []string{} | |
for i := 0; i < len(data); i++ { | |
j := i % 8 | |
if j == 0 && i != 0 { | |
k++ | |
rows[k] = strings.Join(buf, ", ") | |
buf = []string{} | |
} | |
buf = append(buf, fmt.Sprintf("0x%s", pad(strconv.FormatUint(uint64(data[i]), 16)))) | |
} | |
t.Execute(os.Stdout, struct { | |
Data []string | |
}{Data: rows}) | |
} | |
func pad(s string) string { | |
if len(s) == 1 { | |
return "0" + s | |
} | |
return s | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment